/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 37ms 612.0 KiB
#3 Accepted 107ms 572.0 KiB
#4 Accepted 80ms 536.0 KiB
#5 Accepted 107ms 532.0 KiB
#6 Accepted 197ms 1.434 MiB
#7 Accepted 129ms 5.773 MiB
#8 Accepted 157ms 5.77 MiB
#9 Accepted 282ms 5.77 MiB
#10 Accepted 245ms 5.707 MiB
#11 Accepted 270ms 5.77 MiB
#12 Accepted 274ms 5.648 MiB

Code

#include <bits/stdc++.h>
#define int long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;


const int mod = 1e9 + 7, N = 1e6;
 
void solve()
{
    int n , k; cin >> n >> k;
    int ar[n + 3];
    multiset<int> st, my;
    for(int i = 1; i <= n; i++) {
        cin >> ar[i];
        st.insert(ar[i]);

    }
    int sm = 0;
    int ans = 1e18;
    for(int i = 1; i <= n; i++) {
        sm += ar[i];
        my.insert(ar[i]);
        st.erase(st.find(ar[i]));        
        if(i >= k) {
            ans = min(ans , sm);
            if(st.size()) {
                ans  = min(ans, sm - *(--my.end()) + *(st.begin()));
            }
            sm -= ar[i - k + 1];

            my.erase(my.find (ar[i - k + 1]));
            st.insert(ar[i - k + 1]);
        }
    }
    cout << ans << endll;
}

int32_t main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
}
 return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-26 03:51:01
Judged At
2024-12-26 03:51:01
Judged By
Score
100
Total Time
282ms
Peak Memory
5.773 MiB