結果

問題 No.1248 Kth Sum
ユーザー Quiet_Space_timeQuiet_Space_time
提出日時 2023-01-05 12:24:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,133 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 84,524 KB
実行使用メモリ 11,888 KB
最終ジャッジ日時 2023-08-19 10:17:32
合計ジャッジ時間 13,262 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define fi first
#define sc second
#define mkp make_pair
#define pii pair<int,int>
typedef long long ll;
const int N=2e5+5,oo=1e9,mod=998244353,G=3,invG=(mod+1)/G;
inline int read() {
    int x=0,flag=0;char ch=getchar();
    while(ch<'0'||ch>'9') {flag|=(ch=='-');ch=getchar();}
    while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return flag?-x:x;
}
inline int mx(int x,int y) {return x>y?x:y;}
inline int mn(int x,int y) {return x<y?x:y;}
inline void swp(int &x,int &y) {x^=y^=x^=y;}
inline int as(int x) {return x>0?x:-x;}

int n,k,a[N],fr[N],L[N],minn[N];
priority_queue<pii> Q1;
priority_queue<int> Q3,Q4;
priority_queue< pii,vector<pii>,greater<pii> > Q2;
int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif
    n=read();k=read();
    for(int i=1;i<=n;++i) a[i]=read();
    for(int i=1;i<=n;++i) {
        fr[i]=(i-2)/(k-1)+1;
        if(!L[fr[i]]) L[fr[i]]=i;
    }
    minn[n]=a[n];
    for(int i=n-1;i>=1;--i) minn[i]=mn(minn[i+1],a[i]);
    ll ans=a[k],sum=0;
    for(int i=n,pos;i>k;--i) {
        if(Q1.size()==fr[i]-1) {
            pos=fr[i]*k;
            if(Q3.top()>=pos) ans=min(ans,a[i]+sum);
            else ans=min(ans,a[i]+sum-Q1.top().fi+minn[pos]);
        }
        Q2.push(mkp(a[i],i));
        while(!Q2.empty()&&Q1.size()<fr[i-1]-1) {
            Q1.push(Q2.top()),sum+=Q2.top().fi;
            Q3.push(Q2.top().sc);
            Q2.pop();
        }
        while(!Q1.empty()&&Q1.size()>fr[i-1]-1) {
            Q2.push(Q1.top()),sum-=Q1.top().fi;
            Q4.push(Q1.top().sc);
            Q1.pop();
        }
        while(!Q1.empty()&&!Q2.empty()&&Q1.top()>Q2.top()) {
            sum=sum-Q1.top().fi+Q2.top().fi;
            Q3.push(Q2.top().sc);Q4.push(Q1.top().sc);
            pii tmp=Q1.top();Q1.pop();
            Q1.push(Q2.top());Q2.pop();
            Q2.push(tmp);
        }
        while(!Q3.empty()&&!Q4.empty()&&Q3.top()==Q4.top()) Q3.pop(),Q4.pop();
    }
    printf("%lld\n",ans);
    return 0;
}
0