結果

問題 No.1248 Kth Sum
ユーザー Quiet_Space_timeQuiet_Space_time
提出日時 2023-01-05 12:28:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,045 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 85,236 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2023-08-19 10:20:51
合計ジャッジ時間 4,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 RE -
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 16 ms
6,276 KB
testcase_14 AC 19 ms
7,432 KB
testcase_15 AC 16 ms
6,452 KB
testcase_16 AC 30 ms
8,048 KB
testcase_17 AC 22 ms
7,604 KB
testcase_18 AC 20 ms
7,524 KB
testcase_19 AC 20 ms
7,340 KB
testcase_20 AC 18 ms
7,504 KB
testcase_21 AC 18 ms
7,512 KB
testcase_22 AC 18 ms
7,556 KB
testcase_23 RE -
testcase_24 AC 59 ms
10,460 KB
testcase_25 AC 82 ms
11,264 KB
testcase_26 AC 49 ms
9,424 KB
testcase_27 AC 12 ms
5,828 KB
testcase_28 AC 12 ms
5,816 KB
testcase_29 RE -
testcase_30 AC 29 ms
8,568 KB
testcase_31 AC 18 ms
7,392 KB
testcase_32 AC 17 ms
7,452 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 43 ms
8,148 KB
testcase_35 AC 25 ms
7,472 KB
testcase_36 AC 21 ms
7,340 KB
testcase_37 AC 20 ms
7,512 KB
権限があれば一括ダウンロードができます

ソースコード

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() {
    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) {
        pos=fr[i]*k;
        if(pos<=n&&Q1.size()==fr[i]-1) {
            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