/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 27ms 572.0 KiB
#3 Wrong Answer 49ms 808.0 KiB

Code

/**
 *
 *    author: Ayon Das Gupta
 *
 **/

#include <bits/stdc++.h>
#define ki(x) cout << x << '\n'
#define debug(v) for(auto &i : v) { cout << i << ' '; } cout << '\n';
#define debug2(v) for(auto &[x, y] : v) { cout << x << ' ' << y << '\n'; } cout << '\n';
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;

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

    int t;
    cin >> t;

    while (t--) {
        int n, k;
        cin >> n >> k;

        vector<int> v(n);
        for (int i = 0; i < n; i++) cin >> v[i];

        vector<int> pref(n, INT_MAX), suff(n, INT_MAX);
        pref[0] = v[0];
        for (int i = 1; i < n; i++) {
            pref[i] = min(pref[i - 1], v[i]);
        }

        suff[n - 1] = v[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            suff[i] = min(suff[i + 1], v[i]);
        }

        map<int, int> mp;
        ll sum = 0;

        for (int i = 0; i < k; i++) {
            sum += v[i];
            mp[v[i]]++;
        }

        ll min1 = sum;

        int p = mp.rbegin()->first;
        sum -= p;
        if (--mp[p] == 0) mp.erase(p);
        if (k < n) {
            min1 = min(min1, sum + suff[k]);
        }
        sum += p;
        int l = 0;
        for (int i = k; i < n; i++) {
            sum -= v[l];
            if (--mp[v[l]] == 0) mp.erase(v[l]);
            sum += v[i];
            mp[v[i]]++;

            min1 = min(min1, sum);


            int tmp = mp.rbegin()->first;
            sum -= tmp;
            if (--mp[tmp] == 0) mp.erase(tmp);
            if (i + 1 < n) {
                min1 = min({min1, sum + pref[l], sum + suff[i + 1]});
            }
            sum += tmp;
            mp[tmp]++;
            l++;
        }

        cout << min1 << '\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 12:16:50
Judged At
2024-12-10 12:16:50
Judged By
Score
1
Total Time
49ms
Peak Memory
808.0 KiB