/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 44ms 540.0 KiB
#3 Accepted 87ms 736.0 KiB
#4 Accepted 127ms 572.0 KiB
#5 Accepted 168ms 660.0 KiB
#6 Accepted 222ms 1.633 MiB
#7 Accepted 22ms 824.0 KiB
#8 Accepted 23ms 1.027 MiB
#9 Accepted 317ms 3.777 MiB
#10 Accepted 265ms 4.137 MiB
#11 Accepted 303ms 3.594 MiB
#12 Accepted 249ms 2.734 MiB

Code

/*
    Author    : MishkatIT
    Created   : Tuesday 10-12-2024 15:38:30
*/

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

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

using ll = long long;
using ld = long double;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
const ll linf = 1e18;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int tc;
    cin >> tc;
    while (tc--) {
        int n, k;
        cin >> n >> k;
        vector<int> v(n);
        for (auto& i : v) cin >> i;
        map<int, int> cur, out;
        ll sum = 0;
        ll ans = linf;
        for (auto& i : v) {
            out[i]++;
        }
        for (int i = 0; i < n; i++) {
            sum += v[i];
            cur[v[i]]++;
            out[v[i]]--;
            if (out[v[i]] == 0) {
                out.erase(v[i]);
            }
            if (i >= k - 1) {
                // debug(i);
                // debug(out);
                ans = min(ans, sum);
                if (out.size() > 0) {
                    sum -= cur.rbegin()->first;
                    sum += out.begin()->first;
                    ans = min(ans, sum);
                    sum += cur.rbegin()->first;
                    sum -= out.begin()->first;
                }
                out[v[i - k + 1]]++;
                cur[v[i - k + 1]]--;
                if (cur[v[i - k + 1]] == 0) {
                    cur.erase(v[i - k + 1]);
                }
                sum -= v[i - k + 1];
            }
            // debug(ans);
        }
        cout << ans << '\n';
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 15:49:14
Judged At
2024-12-10 15:49:14
Judged By
Score
100
Total Time
317ms
Peak Memory
4.137 MiB