/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 26ms 808.0 KiB
#3 Accepted 43ms 660.0 KiB
#4 Accepted 48ms 540.0 KiB
#5 Accepted 55ms 1.059 MiB
#6 Accepted 109ms 6.785 MiB
#7 Accepted 197ms 34.938 MiB
#8 Accepted 207ms 34.926 MiB
#9 Accepted 211ms 34.945 MiB
#10 Accepted 191ms 35.055 MiB
#11 Accepted 215ms 35.004 MiB
#12 Accepted 202ms 34.812 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; vector<int> lg;  function<T(T, T)> ftn; 
public: SPT_mn(const vector<T>& arr, function<T(T, T)> func) {
        int n = arr.size(), k = log2(n) + 1; ftn = func;
        lg.resize(n + 1); lg[1] = 0; 
        for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 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] = ftn(mr[i][j - 1], mr[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    T query(int L, int R) {
        int j = lg[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; vector<int> lg;  function<T(T, T)> ftn; 
public: SPT_mx(const vector<T>& arr, function<T(T, T)> func) {
        int n = arr.size(), k = log2(n) + 1; ftn = func;
        lg.resize(n + 1); lg[1] = 0; 
        for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 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] = ftn(mr[i][j - 1], mr[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    T query(int L, int R) {
        int j = lg[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:54:19
Judged At
2024-12-10 16:54:19
Judged By
Score
100
Total Time
215ms
Peak Memory
35.055 MiB