/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 540.0 KiB
#2 Wrong Answer 18ms 840.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 to calculate minimum
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] = min(seg[2 * index + 1], seg[2 * index + 2]);  // Use min for range queries
}

int range_query(int index, int low, int high, int l, int r, vector<int> &seg) {
    /// no overlap -> [low high] [l r] or [l r] [low high]
    if(high < l or r < low) return INT_MAX;

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

    /// partial overlap -> [2 5] [4 6]
    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 min(left, right);  // Return the minimum of the range
}

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

        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]);
        }

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

        ll min1 = sum;

        // Sliding window logic, subtracting the minimum element from range [0, k-1]
        sum -= range_query(0, 0, n - 1, 0, k - 1, seg);

        min1 = min(min1, sum + suff[k]);

        // Add back the minimum element in the range
        sum += range_query(0, 0, n - 1, 0, k - 1, seg);

        int l = 0;
        for (int i = k; i < n; i++) {
            sum -= v[l];  // Remove the left-most element of the previous window
            sum += v[i];  // Add the new right-most element

            min1 = min(min1, sum);

            // Find the minimum value in the range from l+1 to i using the segment tree
            int tmp = range_query(0, 0, n - 1, l + 1, i, seg);

            sum -= tmp;  // Subtract the range's minimum value

            min1 = min({min1, sum + pref[l], sum + suff[i + 1]});

            sum += tmp;  // Add back the range's minimum value
            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:17:30
Judged At
2024-12-10 14:17:30
Judged By
Score
0
Total Time
18ms
Peak Memory
840.0 KiB