/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 584.0 KiB
#2 Accepted 39ms 572.0 KiB
#3 Accepted 72ms 816.0 KiB
#4 Accepted 91ms 580.0 KiB
#5 Accepted 105ms 664.0 KiB
#6 Accepted 154ms 1.625 MiB
#7 Accepted 117ms 5.812 MiB
#8 Accepted 158ms 6.07 MiB
#9 Accepted 270ms 5.902 MiB
#10 Accepted 251ms 5.898 MiB
#11 Accepted 266ms 5.906 MiB
#12 Accepted 299ms 5.902 MiB

Code

#include <bits/stdc++.h>
using namespace std;
// #define ll long long 
#define nl '\n'
#define f(i,s,n) for(int i = s; i < n; i++)
#define int long long

void solve(){
   int n,k;
   cin >> n >> k;
   vector<int > arr(n);
   multiset<int> ms, msMx;
   f(i,0,n) {cin >> arr[i];ms.insert(arr[i]);}
   int ptr1 = 0, ptr2 = 0, ans = 1e12, sm = 0;
   
   auto upd = [&](){
    ans = min(ans, sm);
    sm -= *msMx.rbegin();
    // cout << *msMx.rbegin() << nl;
    if(ms.size() == 0)return;
    sm += *ms.begin();
    ans = min(ans, sm);
    sm -= *ms.begin();
    sm += *msMx.rbegin();
   };
   while(ptr2 < n){
    
    while(ptr2 < n && ptr2 - ptr1 < k){
        ms.erase(ms.find(arr[ptr2]));
        sm += arr[ptr2];
        msMx.insert(arr[ptr2]);
        ptr2++;
    }
    if(ptr2 > n || ptr2 - ptr1 < k)break;
    upd();
    ms.insert(arr[ptr1]);
    msMx.erase(msMx.find(arr[ptr1]));
    sm -= arr[ptr1];
    ptr1++;

   }
   cout<<ans<<nl;
   
}

signed main() {
   ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
   int T = 1;
   cin>>T;
   for(int tc = 1; tc <= T; tc++){
        solve();
   }
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-09 05:33:51
Judged At
2024-12-09 05:33:51
Judged By
Score
100
Total Time
299ms
Peak Memory
6.07 MiB