/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 332.0 KiB
#2 Wrong Answer 24ms 572.0 KiB
#3 Wrong Answer 40ms 836.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 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];
            l++;
            mst.erase(mst.find(v[l]));
        }
        if(r-l+1==k)
        {
            // cout<<sum<<" "<<*(--mst.end())<<" ";
            // if(l==0)    cout<<"lnai "<<pore[r+1]<<nl;
            // else if(r==n-1)    cout<<age[l-1]<<" rnai"<<nl;
            // else cout<<age[l-1]<<" "<<pore[r+1]<<nl;

            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;

//     // vector<ll>pre(n);
//     // pre[0]=v[0];
//     // for(ll i=1;i<n;i++)
//     // {
//     //     pre[i]=pre[i-1]+v[i];
//     // }
//     // ll mx=LLONG_MIN;
//     // multiset<ll>ms;
//     // for(ll i=0;i<k;i++)
//     // {
//     //     mx=max(mx, v[i]);
//     //     ms.insert(v[i]);
//     // }
//     // ll ans=LLONG_MAX;
//     // ll x=pre[k-1];
//     // ans=min(ans, x);
//     // x=pre[k-1]-*(--ms.end())+pore[k];
//     // ll p=pre[k-1];
//     // // cout<<ans<<nl;
//     // for(ll i=k, j=0;i<n;i++, j++)
//     // {
//     //     p+=v[i]; p-=v[j];
//     //     ans=min(ans, p);
//     //     ms.erase(ms.find(v[j]));
//     //     ms.insert(v[i]);
//     //     p+=*(--ms.end());
//     //     ans=min(ans, p);
//     // }
//     // 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 07:00:13
Judged At
2024-12-09 07:00:13
Judged By
Score
1
Total Time
40ms
Peak Memory
836.0 KiB