/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 14ms 624.0 KiB
#3 Wrong Answer 23ms 596.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define bug(a) cout<<#a<<" : "<<a<<endl;
void solve(){
    int n, k; cin>>n>>k;
    vector<int> v(n+1), pre(n+1);
    for(int i = 1; i <= n; ++i) cin>>v[i];
    for(int i = 1; i <= n; ++i) {
        pre[i] = pre[i-1]+v[i];
    }
    int j = k;
    int ans = 1e6, a = -1, b = -1;
    for(int i = 1; i <= n-k+1; ++i, ++j){
        int res = pre[j] - pre[i-1];
        if(res <= ans){
            ans = res;
            a = i, b = j;
            int mx = -1e6, mn = 1e6;
            for(int i = a; i <= b; ++i){
                mx = max(v[i], mx);
            }
            // bug(mx);
            for(int i = 1; i <= n; ++i){
                if(i == a){
                    i += b-1;
                    // bug(i);
                    continue;
                }
                mn = min(v[i], mn);
            }
            int prev_ans = ans;
            ans -= mx;
            ans += mn;
            if(prev_ans < ans){
                ans = prev_ans;
            }
        }
    }
    cout<<ans<<endl;
    // bug(ans);
    // bug(a);
    // bug(b);
    // int mx = -1e6, mn = 1e6;
    // for(int i = a; i <= b; ++i){
    //     mx = max(v[i], mx);
    // }
    // // bug(mx);
    // for(int i = 1; i <= n; ++i){
    //     if(i == a){
    //         i += b-1;
    //         bug(i);
    //         continue;
    //     }
    //     mn = min(v[i], mn);
    // }
    // // bug(mn);
    // int prev_ans = ans;
    // ans -= mx;
    // ans += mn;
    // if(prev_ans < ans){
    //     cout<<prev_ans<<endl;
    // }
    // else{
    //     cout<<ans<<endl;
    // }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    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
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-09 06:44:53
Judged At
2024-12-09 06:44:53
Judged By
Score
1
Total Time
23ms
Peak Memory
624.0 KiB