/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 540.0 KiB
#2 Wrong Answer 35ms 564.0 KiB

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;
    }
    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:17:45
Judged At
2024-12-08 20:17:45
Judged By
Score
0
Total Time
35ms
Peak Memory
564.0 KiB