/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 584.0 KiB
#2 Wrong Answer 20ms 576.0 KiB
#3 Wrong Answer 28ms 660.0 KiB

Code

#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;

/// Segment Tree
void build(int index, int low, int high, vector<int> &v, vector<int> &seg) {
    if(low == high) {
        seg[index] = v[low];
        return;
    }

    int mid = (high + low) >> 1;
    build(2 * index + 1, low, mid, v, seg);
    build(2 * index + 2, mid + 1, high, v, seg);

    seg[index] = max(seg[2 * index + 1], seg[2 * index + 2]);
}

int range_query(int index, int low, int high, int l, int r, vector<int> &seg) {
    if(high < l or r < low) return 0;

    if(low >= l and high <= r) return seg[index];

    int mid = (low + high) >> 1;
    int left = range_query(2 * index + 1, low, mid, l, r, seg);
    int right = range_query(2 * index + 2, mid + 1, high, l, r, seg);
    return max(left, right);
}

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), seg(4 * n);
        for (int i = 0; i < n; i++) cin >> v[i];

        build(0, 0, n - 1, v, seg);

        // Prefix and Suffix arrays to store minimum values
        vector<int> pref(n + 5, INT_MAX), suff(n + 5, 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]);
        }

        // Sum of first k elements
        ll sum = 0;
        for (int i = 0; i < k; i++) {
            sum += v[i];
        }

        // Initial min1 is the sum of the first k elements
        ll min1 = sum;

        // Subtract the maximum value in the range [0, k-1]
        sum -= range_query(0, 0, n - 1, 0, k - 1, seg);

        // Compare with the sum + suff[k] to minimize it
        min1 = min(min1, sum + suff[k]);

        // Add the maximum value back for the next iteration
        sum += range_query(0, 0, n - 1, 0, k - 1, seg);

        int l = 0;
        for (int i = k; i < n; i++) {
            // Slide the window by removing the element at index l and adding the element at index i
            sum -= v[l];
            sum += v[i];

            // Update the minimum sum encountered
            min1 = min(min1, sum);

            // Find the maximum value in the range [l + 1, i]
            int tmp = range_query(0, 0, n - 1, l + 1, i, seg);

            // Remove the maximum value from the sum temporarily
            sum -= tmp;

            // Now try to minimize the sum by considering the minimum values from pref and suff
            min1 = min({min1, sum + pref[l], sum + suff[i + 1]});

            // Add the maximum value back to the sum
            sum += tmp;

            // Move the left pointer forward
            l++;
        }

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