/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 23ms 772.0 KiB
#3 Accepted 41ms 796.0 KiB
#4 Accepted 59ms 576.0 KiB
#5 Accepted 49ms 696.0 KiB
#6 Accepted 63ms 1.684 MiB
#7 Accepted 68ms 7.559 MiB
#8 Accepted 57ms 3.355 MiB
#9 Accepted 77ms 3.102 MiB
#10 Accepted 94ms 5.02 MiB
#11 Accepted 77ms 3.52 MiB
#12 Accepted 76ms 3.27 MiB

Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using o_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define endl '\n'
#define F first
#define S second
#define pii pair<int, int>
#define sz(x) (int) x.size()
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int INF = 1e18 + 10;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    int n, k; cin>>n>>k;
    vector<int> v(n + 1), pf(n + 1), sf(n + 1);
    for(int i = 0; i < n; i++) cin>>v[i];

    for(int i = 0; i < n; i++) pf[i] = min((i - 1 >= 0 ? pf[i - 1] : INF), v[i]);
    for(int i = n; i >= 0; i--) sf[i] = min((i + 1 < n ? sf[i + 1] : INF), v[i]);

    int sm = 0;
    multiset<int> st;
    for(int i = 0; i < k; i++) st.insert(v[i]), sm += v[i];

    int ans = INF;

    for(int i = k; i <= n; i++) {
        ans = min(ans, sm);
        int mx = *(--st.end()), mn = min((i - k - 1 >= 0 ? pf[i - k - 1] : INF), (i + 1 < n ? sf[i + 1] : INF));
        ans = min(ans, sm - mx + mn);
        {
            sm += v[i] - v[i - k];
            st.insert(v[i]);
            st.erase(st.find(v[i - k]));
        }
    }

    cout<<ans<<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1, c = 1; cin>>t;
    while(t--) {
        // cerr<<"Case "<<c++<<": \n";
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-25 10:41:04
Judged At
2024-12-25 10:41:04
Judged By
Score
100
Total Time
94ms
Peak Memory
7.559 MiB