/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 22ms 768.0 KiB
#3 Accepted 39ms 832.0 KiB
#4 Accepted 43ms 580.0 KiB
#5 Accepted 49ms 696.0 KiB
#6 Accepted 61ms 1.406 MiB
#7 Accepted 65ms 6.188 MiB
#8 Accepted 51ms 2.188 MiB
#9 Accepted 76ms 2.273 MiB
#10 Accepted 92ms 3.871 MiB
#11 Accepted 76ms 2.145 MiB
#12 Accepted 74ms 2.121 MiB

Code

#include<bits/stdc++.h>
using namespace std; 
using i64 = long long;

void solve() {
    int n, k; cin >> n >> k;
    vector<int> A(n);
    for (auto& a : A) cin >> a;
    auto pref = A, suf = A;
    for (int i = 1; i < n; i++) {
        pref[i] = min(pref[i], pref[i - 1]);
    }
    for (int i = n - 2; i >= 0; i--) {
        suf[i] = min(suf[i], suf[i + 1]);
    }
    i64 sum = 0, ans = 1e18;
    multiset<int> st;
    for (int i = 0, j = i - k + 1; i < n; i++, j++) {
        sum += A[i];
        st.insert(A[i]);
        if (j >= 0) {   
            ans = min(ans, sum);
            if (j - 1 >= 0) {
                ans = min(ans, sum - *st.rbegin() + pref[j - 1]);
            }
            if (i + 1 < n) {
                ans = min(ans, sum - *st.rbegin() + suf[i + 1]);
            }
            sum -= A[j];
            st.erase(st.find(A[j]));
        }
    }
    cout << ans << '\n';
} 

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt = 1;
    cin >> tt;
    for(int t = 1; t <= tt; t++) {
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 09:35:14
Judged At
2024-12-10 09:35:14
Judged By
Score
100
Total Time
92ms
Peak Memory
6.188 MiB