/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 336.0 KiB
#2 Wrong Answer 24ms 616.0 KiB
#3 Wrong Answer 37ms 604.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define all(v) v.begin(),v.end()
#define nl "\n"
#define FIO \
    ios_base::sync_with_stdio(false);\
    cin.tie(NULL);\
    cout.tie(NULL);

void solve()
{
    ll n,k; cin>>n>>k;
    vector<ll>v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    ll l=0,sum=0,ans=LLONG_MAX;
    ll left=-1,right=-1;
    for(ll r=0;r<n;r++)
    {
        sum+=v[r];
        while(r-l+1>k)
        {
            sum-=v[l];
            l++;
        }
        if(r-l+1==k)
        {
            if(sum<ans)
            {
                ans=sum;
                left=l,right=r;
            }
        }
    }
    // cout<<ans<<nl;
    // cout<<left<<" "<<right<<nl;
    ll mn=LLONG_MAX,mx=LLONG_MIN;
    for(ll i=0;i<n;i++)
    {
        if(i>=left && i<=right)  mx=max(mx,v[i]);
        else    mn=min(mn,v[i]);
    }
    if(mn<mx)   cout<<ans-mx+mn<<nl;
    else    cout<<ans<<nl;
    // cout<<ans<<nl;
    // cout<<mn<<" "<<mx<<nl;
}

int32_t main()
{
 FIO
    ll t=1;
    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        // cout<<"Case "<<i<<": ";
        solve();
    }
}

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:09:23
Judged At
2024-12-09 05:09:23
Judged By
Score
1
Total Time
37ms
Peak Memory
616.0 KiB