/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 14ms 556.0 KiB
#3 Accepted 23ms 584.0 KiB
#4 Accepted 24ms 788.0 KiB
#5 Accepted 64ms 604.0 KiB
#6 Accepted 501ms 1.035 MiB
#7 Accepted 24ms 2.816 MiB
#8 Accepted 1355ms 2.855 MiB
#9 Accepted 1237ms 2.855 MiB
#10 Time Exceeded ≥2098ms ≥2.848 MiB
#11 Accepted 1303ms 2.848 MiB
#12 Accepted 1306ms 2.844 MiB

Code

#include <bits/stdc++.h>
#define nl '\n'
#define ll long long int
#define all(v) v.begin(),v.end()
#define print(v) for(auto data : v) cout << data << " "; cout << nl
using namespace std;
void solve()
{
    ll n, k; cin >> n >> k;
    vector<ll> v(n); for(auto &data : v) cin >> data;

    vector<ll> prefMin(n, LLONG_MAX), suffixMin(n, LLONG_MAX);
    prefMin[0] = v[0];
    for (int i = 1; i < n; i++) prefMin[i] = min(v[i], prefMin[i-1]);
    suffixMin[n-1] = v.back();
    for (int i = n-2; i >= 0; i--) suffixMin[i] = min(v[i], suffixMin[i+1]);
    

    ll ans = LLONG_MAX, sum = 0;
    for (int l = 0, r = 0; r < n; r++)
    {
        sum += v[r];
        if(r-l+1 == k)
        {
            ll mxFromWindow, mnBefore = LLONG_MAX, mnAfter = LLONG_MAX;
            mxFromWindow = *max_element(v.begin() + l, v.begin() + r + 1);
            if(l > 0) mnBefore = prefMin[l-1];
            if(r+1 < n) mnAfter = suffixMin[r+1];
            ll tmp = sum;
            if(mxFromWindow > min(mnBefore, mnAfter))
            {
                tmp -= mxFromWindow; tmp += min(mnBefore, mnAfter); 
            }
            ans = min(ans, tmp);
            sum -= v[l]; l++;
        }
    }
    cout << ans << nl;
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int t; cin >> t; 
    while (t--) solve();

    return 0;
}

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 11:05:33
Judged At
2024-12-10 11:05:33
Judged By
Score
90
Total Time
≥2098ms
Peak Memory
≥2.855 MiB