/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 564.0 KiB
#2 Accepted 64ms 700.0 KiB
#3 Accepted 114ms 712.0 KiB
#4 Accepted 167ms 572.0 KiB
#5 Accepted 142ms 584.0 KiB
#6 Accepted 192ms 2.746 MiB
#7 Accepted 68ms 2.598 MiB
#8 Accepted 74ms 2.785 MiB
#9 Accepted 253ms 6.309 MiB
#10 Accepted 218ms 7.57 MiB
#11 Accepted 271ms 6.387 MiB
#12 Accepted 221ms 5.035 MiB

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];
        mini=min(mini,sum);
        sum = xxx; r++;
        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:49:12
Judged At
2024-12-09 08:49:12
Judged By
Score
100
Total Time
271ms
Peak Memory
7.57 MiB