/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 24ms 628.0 KiB
#3 Accepted 31ms 612.0 KiB
#4 Accepted 43ms 540.0 KiB
#5 Accepted 52ms 664.0 KiB
#6 Accepted 66ms 1.621 MiB
#7 Accepted 29ms 5.812 MiB
#8 Accepted 99ms 5.77 MiB
#9 Accepted 102ms 6.02 MiB
#10 Accepted 72ms 5.867 MiB
#11 Accepted 103ms 5.902 MiB
#12 Accepted 104ms 5.906 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 Stree_mn { private:
    vector<T> Tr; int n; function<T(T, T)> op;
public:
    Stree_mn(const vector<T>& ar, function<T(T, T)> opp) {
        n = ar.size(); op = opp; Tr.resize(2 * n);
        for (int i = 0; i < n; i++) {
            Tr[n + i] = ar[i];
        } build();
    }
    void build() {
        for (int i = n - 1; i > 0; --i) {
            Tr[i] = op(Tr[i << 1], Tr[i << 1 | 1]);
        }
    }
    T query(int l, int r) { l--, r--;
        T r_left = INT_MAX, r_right = INT_MAX;
        for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) r_left = op(r_left, Tr[l++]);
            if (r & 1) r_right = op(Tr[--r], r_right);
        }
        return op(r_left, r_right);
    }
};


template<class T> class Stree_mx { private:
    vector<T> Tr; int n; function<T(T, T)> op;
public:
    Stree_mx(const vector<T>& ar, function<T(T, T)> opp) {
        n = ar.size(); op = opp; Tr.resize(2 * n);
        for (int i = 0; i < n; i++) {
            Tr[n + i] = ar[i];
        } build();
    }
    void build() {
        for (int i = n - 1; i > 0; --i) {
            Tr[i] = op(Tr[i << 1], Tr[i << 1 | 1]);
        }
    }
    T query(int l, int r) { l--, r--;
        T r_left = INT_MIN, r_right = INT_MIN;
        for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) r_left = op(r_left, Tr[l++]);
            if (r & 1) r_right = op(Tr[--r], r_right);
        }
        return op(r_left, r_right);
    }
};

template<class T> class Stree_sum { private:
    vector<T> Tr; int n; function<T(T, T)> op;
public:
    Stree_sum(const vector<T>& ar, function<T(T, T)> opp) {
        n = ar.size(); op = opp; Tr.resize(2 * n);
        for (int i = 0; i < n; i++) {
            Tr[n + i] = ar[i];
        } build();
    }
    void build() {
        for (int i = n - 1; i > 0; --i) {
            Tr[i] = op(Tr[i << 1], Tr[i << 1 | 1]);
        }
    }
    T query(int l, int r) { l--, r--;
        T r_left = 0, r_right = 0;
        for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) r_left = op(r_left, Tr[l++]);
            if (r & 1) r_right = op(Tr[--r], r_right);
        }
        return op(r_left, r_right);
    }
};


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);
        for(int i = 0; i < n; i++) {
            cin >> ar[i];
        }
        Stree_mn<int64_t> smn(ar, [](int64_t a, int64_t b) {
            return min(a, b);
        });
        Stree_mx<int64_t> smx(ar, [](int64_t a, int64_t b) {
            return max(a, b);
        });
        Stree_sum<int64_t> sum(ar, [](int64_t a, int64_t b) {
            return a + b;
        });
        
        int64_t ans = LLONG_MAX;
        for(int i = 1; i <= n - k + 1; i++) {
            int64_t tm = sum.query(i, i + k - 1);
            int64_t mn = LLONG_MAX;
            if(i > 1) {
                mn = min(mn, smn.query(1, i - 1));
            }
            if(i + k - 1 < n) {
                mn = min(mn, smn.query(i + k, n));
            }
            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:47:38
Judged At
2024-12-10 16:47:38
Judged By
Score
100
Total Time
104ms
Peak Memory
6.02 MiB