/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 584.0 KiB
#2 Accepted 35ms 608.0 KiB
#3 Accepted 56ms 580.0 KiB
#4 Accepted 72ms 576.0 KiB
#5 Accepted 85ms 796.0 KiB
#6 Accepted 129ms 1.617 MiB
#7 Accepted 66ms 5.816 MiB
#8 Accepted 143ms 6.02 MiB
#9 Accepted 259ms 6.016 MiB
#10 Accepted 191ms 5.895 MiB
#11 Accepted 247ms 5.812 MiB
#12 Accepted 251ms 6.086 MiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

void solve() {
    ll n, k, cur = 0;
    cin >> n >> k;
    vector<ll> v(n);
    multiset<ll> now, etc;
    for (int i = 0; i < n; i++) {
        ll x; cin >> x;
        v[i] = x;
        if(i < k) cur += x, now.insert(x);
        else etc.insert(x);
    }
    if(n == k) {
        cout << cur << endl;
        return;
    }

    ll ans = cur;
    if(*now.rbegin() > *etc.begin()) {
        ans = min(ans, cur - *now.rbegin() + *etc.begin());
    }
    int l = 0, r = k;
    while(r < n) {
        now.erase(now.find(v[l])); cur -= v[l];
        now.insert(v[r]); cur += v[r];

        etc.erase(etc.find(v[r]));
        etc.insert(v[l]);

        ans = min({ans, cur, cur - *now.rbegin() + *etc.begin()});
        ++l, ++r;
    }
    cout << ans << endl;
    return;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU Divisonal Contest Problem Testing Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-08 20:18:28
Judged At
2024-12-08 20:18:28
Judged By
Score
100
Total Time
259ms
Peak Memory
6.086 MiB