/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 23ms 588.0 KiB
#3 Accepted 45ms 872.0 KiB
#4 Accepted 46ms 576.0 KiB
#5 Accepted 48ms 920.0 KiB
#6 Accepted 62ms 1.57 MiB
#7 Accepted 66ms 7.395 MiB
#8 Accepted 54ms 3.316 MiB
#9 Accepted 76ms 3.305 MiB
#10 Accepted 92ms 5.059 MiB
#11 Accepted 75ms 3.465 MiB
#12 Accepted 79ms 3.312 MiB

Code

// SEC_Infra_Red
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define nl "\n"
#define pb push_back
#define fi first
#define F first
#define pll pair<ll,ll>
#define sc second
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sortv(v) sort(v.begin(), v.end())
#define revv(v) reverse(v.begin(), v.end())
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vinput ll n; cin >> n; vector<ll>v; for(ll i = 0; i <n; i++){ll in; cin >> in; v.push_back(in);}
#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];
    vector<ll>age(n),pore(n);
    ll mn=LLONG_MAX;
    for(ll i=0;i<n;i++)
    {
        age[i]=min(mn,v[i]);
        mn=min(mn,v[i]);
    }
    mn=LLONG_MAX;
    for(ll i=n-1;i>=0;i--)
    {
        pore[i]=min(mn,v[i]);
        mn=min(mn,v[i]);
    }

    ll l=0,sum=0,ans=LLONG_MAX;
    multiset<ll>mst;
    for(ll r=0;r<n;r++)
    {
        sum+=v[r];
        mst.insert(v[r]);
        while(r-l+1>k)
        {
            sum-=v[l];
            mst.erase(mst.find(v[l]));
            l++;
        }
        if(r-l+1==k)
        {

            if(l==0 && r==n-1)
            {
                ans=min(ans,sum);
            }
            else if(l==0)
            {
                if(*(--mst.end())>pore[r+1])    ans=min(ans,(sum-*(--mst.end())+pore[r+1]));
                else ans=min(ans,sum);
            }
            else if(r==n-1)
            {
                if(*(--mst.end())>age[l-1])    ans=min(ans,(sum-*(--mst.end())+age[l-1]));
                else ans=min(ans,sum);
            }
            else
            {
                if(*(--mst.end())>min(age[l-1],pore[r+1]))  ans=min(ans,(sum-*(--mst.end())+min(age[l-1],pore[r+1])));
                else ans=min(ans,sum);
            }
        }
    }
    cout<<ans<<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 08:06:50
Judged At
2024-12-09 08:06:50
Judged By
Score
100
Total Time
92ms
Peak Memory
7.395 MiB