/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 47ms 568.0 KiB
#3 Wrong Answer 77ms 788.0 KiB

Code

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

long long solve(vector<int> &v, int k) {
    int n = v.size();
    vector<long long> p_sum(n);
    vector<int> p_min(n), s_min(n);
    p_sum[0] = p_min[0] = v[0];
    for(int i = 1; i < n; i++) {
        p_sum[i] = p_sum[i - 1] + v[i];
        p_min[i] = min(p_min[i - 1], v[i]);
    }
    s_min[n - 1] = v[n - 1];
    for(int i = n - 2; i >= 0; i--) {
        s_min[i] = min(s_min[i + 1], v[i]);
    }

    deque<int> dq;
    long long best = 0;
    for(int i = 0; i < n; i++) {
        while(dq.size() > 0 && v[dq.back()] <= v[i]) {
            dq.pop_back();
        }
        dq.push_back(i);

        if(i >= k - 1) {
            int l = i - k + 1;
            int r = i;

            long long r_sum = p_sum[r] - (l > 0 ? p_sum[l - 1] : 0);
            best = min(best, r_sum);
            
            if(k != n) {
                int r_max = v[dq.front()];
                int other_min = min(l > 0 ? p_min[l - 1] : INT_MAX, r < n - 1 ? s_min[r + 1] : INT_MAX);
                best = min(best, r_sum + other_min - r_max); 
            }
            if(dq.front() == i - k + 1) {
                dq.pop_front();
            }
        }
    }

    return best;
}

int main() {
    int tc;
    cin >> tc;
    assert(tc >= 1 && tc <= 10000);
    int total_n = 0;
    while(tc--) {
        int n, k;
        cin >> n >> k;
        total_n += n;
        assert(n >= 1 && n <= 100000);
        assert(k >= 1 && k <= n);
        vector<int> v(n);
        for(int i = 0; i < n; i++) {
            cin >> v[i];
            assert(v[i] >= -100000 && v[i] <= 100000);
        }
        cout << solve(v, k) << endl;
    }

    assert(total_n <= 200000);
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU Divisonal Contest Problem Testing Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-07 18:50:34
Judged At
2024-12-07 18:50:34
Judged By
Score
1
Total Time
77ms
Peak Memory
788.0 KiB