/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 30ms 796.0 KiB
#3 Accepted 38ms 592.0 KiB
#4 Accepted 42ms 772.0 KiB
#5 Accepted 47ms 1.035 MiB
#6 Accepted 97ms 6.645 MiB
#7 Accepted 137ms 34.086 MiB
#8 Accepted 150ms 34.223 MiB
#9 Accepted 144ms 34.141 MiB
#10 Accepted 141ms 34.289 MiB
#11 Accepted 151ms 34.309 MiB
#12 Accepted 148ms 34.012 MiB

Code

#include <bits/stdc++.h>     //   All praise is due to Allah alone, and peace and blessings be
using namespace std;         //         upon him, after whom there is no other Prophet.


template<class T> class SPT_mn { private: vector<vector<T>> mr;
public: SPT_mn(const vector<T>& arr){
        int n = arr.size(), k = log2(n) + 1;
        mr = vector<vector<T>>(n, vector<T>(k));
        for (int i = 0; i < n; i++) mr[i][0] = arr[i];
        for (int j = 1; j < k; j++) {
            for (int i = 0; i + (1 << j) - 1 < n; i++) {
                mr[i][j] = min(mr[i][j - 1], mr[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    T query(int L, int R) {
        int j = 31 - __builtin_clz(R - L + 1);
        return min(mr[L][j], mr[R - (1 << j) + 1][j]);
    }
};

template<class T> class SPT_mx { private: vector<vector<T>> mr;
public: SPT_mx(const vector<T>& arr){
        int n = arr.size(), k = log2(n) + 1;
        mr = vector<vector<T>>(n, vector<T>(k));
        for (int i = 0; i < n; i++) mr[i][0] = arr[i];
        for (int j = 1; j < k; j++) {
            for (int i = 0; i + (1 << j) - 1 < n; i++) {
                mr[i][j] = max(mr[i][j - 1], mr[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    T query(int L, int R) {
        int j = 31 - __builtin_clz(R - L + 1);
        return max(mr[L][j], mr[R - (1 << j) + 1][j]);
    }
};

int32_t main() {
    cin.tie(0)->sync_with_stdio(false);

    function<void()> M_test_case = [&]() {
        int n, k; cin >> n >> k;
        vector<int64_t> ar(n), br(n);
        for(int i = 0; i < n; i++) {
            cin >> ar[i];
        }
        SPT_mn<int64_t> smn(ar);
        SPT_mx<int64_t> smx(ar);
        partial_sum(ar.begin(), ar.end(), br.begin());
        int64_t ans = LLONG_MAX;
        for(int i = 0; i < n - k + 1; i++) {
            int64_t tm = br[i + k - 1];
            int64_t mn = LLONG_MAX;
            if(i) {
                tm -= br[i - 1];
                mn = min(mn, smn.query(0, i - 1));
            }
            if(i + k < n) {
                mn = min(mn, smn.query(i + k, n - 1));
            }
            int64_t mx = smx.query(i, i + k - 1);
            if(mx > mn) {
                tm = tm - mx + mn;
            }
            ans = min(ans, tm);
        }
        cout << ans << '\n';
    };

    int32_t _ = 1;    cin >> _;
    for (int T = 1; T <= _; T++) {
        M_test_case();
    }
    return _ ^ _;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 16:57:14
Judged At
2024-12-10 16:57:14
Judged By
Score
100
Total Time
151ms
Peak Memory
34.309 MiB