/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 328.0 KiB
#2 Wrong Answer 65ms 524.0 KiB
#3 Wrong Answer 140ms 772.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	int t; cin >> t;
	while (t--) {
		ll n, k; cin >> n >> k;
		ll a[n + 5];
		for (ll i = 1; i <= n; i++) cin >> a[i];
		ll mp[n + 5], ms[n + 5];
		mp[0] = 1e18, mp[n + 1] = 1e18;
		for (ll i = 1; i <= n; i++) {
           mp[i] = min(mp[i - 1], a[i]);
		}
		ms[0] = 1e18; ms[n + 1] = 1e18;
		for (ll i = n; i >= 1; i--) {
			ms[i] = min(ms[i + 1], a[i]); 
		}
        ll sum = 0;
        ll mx = -1e18;
        map<ll, ll>mppp; set<ll>se;
        for (ll i = 1; i <= k; i++) {
           se.insert(a[i]); mppp[a[i]]++;
           sum += a[i];
           mx = max(mx, a[i]);
        } //
        ll mini = sum; //cout << mini << '\n';
        ll xxx = sum;
         
        ll l = 1, r = k + 1; //mini = min(mini, sum - (ms[r] - mx));  r++; //cout << sum << '\n';  
        sum -= mx; sum += ms[r];
        sum = xxx;
        for (int i = k + 1; i <= n; i++) {
            ll ult_min;
             if (i == n) ult_min = mp[l];
             else ult_min = min(mp[l], ms[r]); 
            if (a[i] < mx) { 
                mppp[a[l]]--;
                //for (auto x: mppp) cout << x.first << ' ' << x.second << '\n'; cout << '\n';
              
                if (mppp[a[l]] <= 0) se.erase(a[l]);
                mppp[a[i]]++; se.insert(a[i]);
                auto it = se.end(); --it; 
                mx = *it; 
            }else {
                mx = a[i];
                mppp[a[l]]--;
                if (mppp[a[l]] <= 0) se.erase(a[l]);
                se.insert(a[i]);
                  
            } 
            
            sum -= a[l]; 
            sum += a[i]; xxx = sum; 
            mini = min(mini, sum);
            sum -= mx;
            sum += ult_min;
            mini = min(mini, sum);
            l++; r++; //break;
            sum = xxx;
        } 
        cout << mini << '\n';
		
	}
}

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:21:17
Judged At
2024-12-09 08:21:17
Judged By
Score
1
Total Time
140ms
Peak Memory
772.0 KiB