/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 20ms 776.0 KiB
#3 Wrong Answer 35ms 848.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 1
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
    if (high < l or r < low) return 0;

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

    // partial overlap
    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);
}

/// segment tree 2
void build2(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;
    build2(2 * index + 1, low, mid, v, seg);
    build2(2 * index + 2, mid + 1, high, v, seg);

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

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

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

    // partial overlap
    int mid = (low + high) >> 1;
    int left = range_query2(2 * index + 1, low, mid, l, r, seg);
    int right = range_query2(2 * index + 2, mid + 1, high, l, r, seg);
    return min(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), seg2(4 * n);
        for (int i = 0; i < n; i++) cin >> v[i];

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

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

        if (k < n)
            min1 = min(min1, sum + range_query2(0, 0, n - 1, k, n - 1, seg2));
        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;

            ll option1 = (l <= n - 1) ? range_query2(0, 0, n - 1, 0, l, seg2) : INT_MAX;
            ll option2 = (i + 1 < n) ? range_query2(0, 0, n - 1, i + 1, n - 1, seg2) : INT_MAX;

            min1 = min({min1, sum + option1, sum + option2});
            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 15:57:40
Judged At
2024-12-10 15:57:40
Judged By
Score
1
Total Time
35ms
Peak Memory
848.0 KiB