/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 572.0 KiB
#2 Accepted 25ms 772.0 KiB
#3 Accepted 42ms 636.0 KiB
#4 Accepted 47ms 792.0 KiB
#5 Accepted 53ms 1.285 MiB
#6 Accepted 104ms 6.664 MiB
#7 Accepted 159ms 34.082 MiB
#8 Accepted 189ms 34.02 MiB
#9 Accepted 184ms 34.09 MiB
#10 Accepted 185ms 34.137 MiB
#11 Accepted 199ms 34.332 MiB
#12 Accepted 177ms 34.344 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; function<T(T, T)> ftn;
public: SPT_mn(const vector<T>& arr, function<T(T, T)> func = [](T a, T b) { return min(a, b); }){
        int n = arr.size(), k = log2(n) + 1; ftn = func;
        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] = ftn(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 ftn(mr[L][j], mr[R - (1 << j) + 1][j]);
    }
};

template<class T> class SPT_mx { private: vector<vector<T>> mr; function<T(T, T)> ftn;
public: SPT_mx(const vector<T>& arr, function<T(T, T)> func = [](T a, T b) { return min(a, b); }){
        int n = arr.size(), k = log2(n) + 1; ftn = func;
        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] = ftn(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 ftn(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, [](int64_t a, int64_t b) {
            return min(a, b);
        });
        SPT_mx<int64_t> smx(ar, [](int64_t a, int64_t b) {
            return max(a, b);
        });
        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:43:38
Judged At
2024-12-10 16:43:38
Judged By
Score
100
Total Time
199ms
Peak Memory
34.344 MiB