/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 40ms 768.0 KiB
#3 Wrong Answer 71ms 588.0 KiB

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 = 1e18, sm = 0;
   
   auto upd = [&](){
    ans = min(ans, sm);
    sm -= *msMx.rbegin();
    // cout << *msMx.rbegin() << nl;
    sm += *ms.begin();
    ans = min(ans, sm);
    sm -= *ms.begin();
    sm += *msMx.rbegin();
   };
   while(ptr2 < n){
    
    while(ptr2 - ptr1 < k){
        ms.erase(ms.find(arr[ptr2]));
        sm += arr[ptr2];
        msMx.insert(arr[ptr2]);
        ptr2++;
    }
    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:22:34
Judged At
2024-12-09 05:22:34
Judged By
Score
1
Total Time
71ms
Peak Memory
768.0 KiB