/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 18ms 572.0 KiB
#3 Wrong Answer 28ms 604.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;

/// 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) {
    /// no overlap -> [low high] [l r] or [l r] [low high]
    if(high < l or r < low) return 0;

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

        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;
        sum -= range_query(0, 0, n - 1, 0, k - 1, seg);

        min1 = min(min1, sum + suff[k]);
        sum += range_query(0, 0, n - 1, 0, k - 1, seg);
        int l = 0;
        for (int i = k; i < n; i++) {
            sum -= v[l];

            sum += v[i];

            min1 = min(min1, sum);

            int tmp = range_query(0, 0, n - 1, l + 1, i, seg);

            sum -= tmp;

            min1 = min({min1, sum + pref[l], sum + suff[i + 1]});
            sum += tmp;
            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:15:46
Judged At
2024-12-10 14:15:46
Judged By
Score
1
Total Time
28ms
Peak Memory
604.0 KiB