/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Wrong Answer 65ms 608.0 KiB
#3 Wrong Answer 109ms 628.0 KiB

Code

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std ;
using namespace __gnu_pbds;
template<typename T>using o_set=tree<T,null_type,std::less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define int long long
#define opt() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int N = 2e5+10 ;
const int mod= 1e9+7 ;
#define pb push_back
#define mp make_pair

int32_t main()
{
    opt();
    int tt=1;
    cin>>tt;
    for(int tc=1; tc<=tt; tc++)
    {
        int n,k;
        cin>>n>>k;
        vector<int>v(n);
        for(int i=0; i<n; i++)cin>>v[i];
        vector<int>vk(k);
        int sum=0;
        for(int i=0; i<k; i++)
        {
            sum+=v[i];
        }
        vector<pair<int,int>>vec;
        vec.pb({sum,0});
        int p=1;
        for(int i=1; i<n-k+1; i++)
        {
            sum-=v[i-1];
            sum+=v[i+k-1];
            vec.pb({sum,i});
            p++;
        }


        sort(vec.begin(),vec.end());


        o_set<pair<int,int>>mxx;
        for(int i=0;i<k;i++)
        {
            mxx.insert({v[i],i});
        }
        vector<int>mxAns;
        mxAns.pb(mxx.find_by_order(k-1)->first);
        for(int i=1;i<n-k+1;i++)
        {
            mxx.erase({v[i-1],i-1});
            mxx.insert({v[i+k-1],i+k-1});
            //cout<<v[i+k-1]<<" "<<i+k-1<<endl;
            //cout<<
            mxAns.pb(mxx.find_by_order(k-1)->first);
        }

         int sample=vec[0].first;
        vector<int>smpl;
        smpl.pb(vec[0].second);
        for(int i=1;i<vec.size();i++)
        {
            if(sample==vec[i].first)
            {
                smpl.pb(vec[i].second);
            }
        }
        int ded=-1e18;
        int pos=-1;
        for(int i=0;i<smpl.size();i++)
        {
            if(ded<mxAns[smpl[i]])
            {
                pos=smpl[i];
                ded=max(ded,mxAns[smpl[i]]);
            }


        }

       // cout<<ded<<endl;
       // cout<<"MAX"<<endl;
        //int x=vec[pos].second;
        vector<int>tmp(k);
        int j=0;
        for(int i=pos; i<pos+k; i++)
        {
            tmp[j]=v[i];
            j++;
        }

        //for(int i=0;i<mxAns.size();i++)cout<<mxAns[i]<<endl;
        sort(tmp.begin(),tmp.end());
        sort(v.begin(),v.end());
        int get=-1e18;
        bool f=false;
        for(int i=0; i<k; i++)
        {
            if(v[i]<tmp[i])
            {
                get=v[i];
                f=true;
                break;

            }
        }
       // for(int i=0;i<vec.size();i++)cout<<vec[i].first<<" "<<vec[0].second<<endl;

        int sum3=vec[0].first;
        if(f)
        {
            //sum3=vec[0].first;
            int sm1=tmp.back();
            sum3-=sm1;
            sum3+=get;
        }
        cout<<sum3<<endl;

    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024
Language
C++11 (G++ 13.2.0)
Submit At
2024-12-09 07:20:36
Judged At
2024-12-09 07:20:36
Judged By
Score
1
Total Time
109ms
Peak Memory
628.0 KiB