/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 16ms 732.0 KiB
#3 Accepted 23ms 604.0 KiB
#4 Accepted 33ms 540.0 KiB
#5 Accepted 163ms 580.0 KiB
#6 Accepted 1590ms 724.0 KiB
#7 Accepted 22ms 1.352 MiB
#8 Time Exceeded ≥2096ms ≥1.32 MiB
#9 Time Exceeded ≥2099ms ≥1.277 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;
int cs = 1;
void solve()
{
    // cout << "case: " << cs++ << nl;
    ll n, k; cin >> n >> k;
    vector<ll> v(n); for(auto &data : v) cin >> data;

    ll ans = LLONG_MAX, sum = 0;
    for (int l = 0, r = 0; r < n; r++)
    {
        // cout << l << " ";
        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 = *min_element(v.begin(), v.begin() + l);
            if(r+2 < n) mnAfter = *min_element(v.begin() + r + 1, v.end());
            ll tmp = sum;
            if(mxFromWindow > min(mnBefore, mnAfter))
            {
                // cout << "#######################" << nl;
                tmp -= mxFromWindow; tmp += min(mnBefore, mnAfter); // sum e problem
            }
            // cout << mnBefore << " " << mxFromWindow << " " << mnAfter << nl;
            ans = min(ans, tmp);
            // cout << "Sum : " << sum << " <-> " << "ans : " << ans << nl;
            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 10:48:53
Judged At
2024-12-10 10:48:53
Judged By
Score
60
Total Time
≥2099ms
Peak Memory
≥1.352 MiB