/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 376.0 KiB
#2 Wrong Answer 46ms 580.0 KiB
#3 Wrong Answer 72ms 620.0 KiB

Code

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

typedef long long ll;
#define bug(a) cout<<#a<<" : "<<a<<endl;
#define bug2(a,b) cout<<#a<<" : "<<a<<"\t"<<#b<<" : "<<b<<endl;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    ll t=1;
    cin>>t;
    ll cs=1;
    while(t--){
        ll n,k;cin>>n>>k;
        vector<ll>v(n);
        for(ll i=0;i<n;i++)cin>>v[i];
        multiset<ll>st;
        ll ans=1e13;
        ll sum=0;
        for(ll i=0;i<k;i++){
            st.insert(v[i]);
            sum+=v[i];
        }
        if(k==n)
        {
            cout<<sum<<endl;
            continue;
        }
        ans=min(ans,sum);
        
        vector<ll>pref(n),suff(n);
        ll mn=1e9;
        for(ll i=0;i<n;i++){
            mn=min(mn,v[i]);
            pref[i]=mn;
        }
        mn=1e9;
        for(ll i=n-1;i>=0;i--){
            mn=min(mn,v[i]);
            suff[i]=mn;
        }
        ll curans=sum+suff[k]-*st.rbegin();
        ans=min(ans,curans);
        // for(ll i=0;i<n;i++){
        //     cout<<pref[i]<<' ';
        // }
        // cout<<endl;
        // for(ll i=0;i<n;i++){
        //     cout<<suff[i]<<' ';
        // }
        // cout<<endl;
        for(ll i=k;i<n-1;i++){
            sum+=v[i];
            sum-=v[i-k];
            ans=min(ans,sum);
            ll curlmn=pref[i-1];
            ll currmn=suff[i+1];
            ll curmn=min(curlmn,currmn);
            st.erase(st.find(v[i-k]));
            st.insert(v[i]);
            ll curans=sum-*st.rbegin()+curmn;
            ans=min(ans,curans);
        }
        st.erase(st.find(v[n-1-k]));
        st.insert(v[n-1]);
        sum += v[n-1];
        sum -= v[n-1-k];
        ans = min(ans,sum);
        curans=sum+pref[n-k-1]-*st.rbegin();
        ans=min(ans,curans);
        cout<<ans<<endl;
    }
    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 05:35:13
Judged At
2024-12-09 05:35:13
Judged By
Score
1
Total Time
72ms
Peak Memory
620.0 KiB