/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 588.0 KiB
#2 Accepted 38ms 568.0 KiB
#3 Accepted 70ms 588.0 KiB
#4 Accepted 83ms 572.0 KiB
#5 Accepted 100ms 540.0 KiB
#6 Accepted 158ms 1.809 MiB
#7 Accepted 118ms 6.031 MiB
#8 Accepted 163ms 5.895 MiB
#9 Accepted 296ms 5.902 MiB
#10 Accepted 295ms 5.895 MiB
#11 Accepted 274ms 5.906 MiB
#12 Accepted 276ms 5.895 MiB

Code

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define endl '\n'
#define pi 2*acos(0.0)




void solve() 
{

    ll n, k; cin >> n >> k;
    ll a[n + 1]; a[0] = 0;
    multiset<ll>out, in;

    for (ll i = 1; i <= n; i++) {
        cin >> a[i];
        out.insert(a[i]);
    }

    ll ans = LONG_LONG_MAX;
    ll sum = 0;
    for (ll i = 1; i <= n; i++) {
        in.insert(a[i]);
        out.erase(out.find(a[i]));
        sum += a[i];

        if (i >= k) {
            if (i > k) {
                in.erase(in.find(a[i - k]));
                sum -= a[i - k];
                out.insert(a[i - k]);
            }
            ll mx = *in.rbegin();
            ll mn = *out.begin();
            //cerr << sum << " " << mx << " " << mn << endl;
            //cerr << mx << " " << mn << endl;
            if (out.size()) {
                ll sum2 = min(sum, sum - mx + mn);
                ans = min(sum2, ans);
            }
            else {
                ans = min(sum, ans);
            }

        }
 

    }

    cout << ans << endl;


}

int main () 
{

    ios::sync_with_stdio(false); cin.tie(nullptr);
    

    ll t = 1; 
    cin >> t;
    while(t--) {
        solve();
    }

    return 0;


}

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 08:46:40
Judged At
2024-12-09 08:46:40
Judged By
Score
100
Total Time
296ms
Peak Memory
6.031 MiB