/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 46ms 768.0 KiB
#3 Wrong Answer 90ms 636.0 KiB

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]]--;
                sum -= v[i - k + 1];
            }
            // debug(ans);
        }
        cout << ans << '\n';
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 10:11:03
Judged At
2024-12-10 10:11:03
Judged By
Score
1
Total Time
90ms
Peak Memory
768.0 KiB