結果

問題 No.1867 Partitions and Inversions
ユーザー Shuai ZuiShuai Zui
提出日時 2023-12-27 10:14:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 772 ms / 5,000 ms
コード長 2,004 bytes
コンパイル時間 2,910 ms
コンパイル使用メモリ 183,180 KB
実行使用メモリ 70,880 KB
最終ジャッジ日時 2023-12-27 10:15:06
合計ジャッジ時間 32,862 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,748 KB
testcase_01 AC 3 ms
7,748 KB
testcase_02 AC 731 ms
70,880 KB
testcase_03 AC 747 ms
70,880 KB
testcase_04 AC 737 ms
70,880 KB
testcase_05 AC 754 ms
70,880 KB
testcase_06 AC 745 ms
70,880 KB
testcase_07 AC 731 ms
70,880 KB
testcase_08 AC 767 ms
70,880 KB
testcase_09 AC 732 ms
70,880 KB
testcase_10 AC 748 ms
70,880 KB
testcase_11 AC 713 ms
70,880 KB
testcase_12 AC 757 ms
70,880 KB
testcase_13 AC 727 ms
70,880 KB
testcase_14 AC 749 ms
70,880 KB
testcase_15 AC 737 ms
70,880 KB
testcase_16 AC 734 ms
70,880 KB
testcase_17 AC 772 ms
70,880 KB
testcase_18 AC 720 ms
70,880 KB
testcase_19 AC 735 ms
70,880 KB
testcase_20 AC 694 ms
70,880 KB
testcase_21 AC 742 ms
70,880 KB
testcase_22 AC 741 ms
70,880 KB
testcase_23 AC 735 ms
70,880 KB
testcase_24 AC 723 ms
70,880 KB
testcase_25 AC 769 ms
70,880 KB
testcase_26 AC 737 ms
70,880 KB
testcase_27 AC 740 ms
70,880 KB
testcase_28 AC 720 ms
70,880 KB
testcase_29 AC 720 ms
70,880 KB
testcase_30 AC 767 ms
70,880 KB
testcase_31 AC 725 ms
70,880 KB
testcase_32 AC 3 ms
7,748 KB
testcase_33 AC 3 ms
9,796 KB
testcase_34 AC 3 ms
9,796 KB
testcase_35 AC 3 ms
9,796 KB
testcase_36 AC 4 ms
9,796 KB
testcase_37 AC 3 ms
9,796 KB
testcase_38 AC 13 ms
17,424 KB
testcase_39 AC 13 ms
17,424 KB
testcase_40 AC 13 ms
17,424 KB
testcase_41 AC 13 ms
17,424 KB
testcase_42 AC 14 ms
17,424 KB
testcase_43 AC 281 ms
49,784 KB
testcase_44 AC 291 ms
49,784 KB
testcase_45 AC 295 ms
49,784 KB
testcase_46 AC 276 ms
49,784 KB
testcase_47 AC 269 ms
49,784 KB
testcase_48 AC 296 ms
49,784 KB
testcase_49 AC 286 ms
49,784 KB
testcase_50 AC 272 ms
49,784 KB
testcase_51 AC 297 ms
49,784 KB
testcase_52 AC 282 ms
49,784 KB
testcase_53 AC 262 ms
49,784 KB
testcase_54 AC 298 ms
49,784 KB
testcase_55 AC 273 ms
49,784 KB
testcase_56 AC 301 ms
49,784 KB
testcase_57 AC 319 ms
49,784 KB
testcase_58 AC 281 ms
49,784 KB
testcase_59 AC 285 ms
49,784 KB
testcase_60 AC 272 ms
49,784 KB
testcase_61 AC 272 ms
49,784 KB
testcase_62 AC 319 ms
49,784 KB
testcase_63 AC 669 ms
70,880 KB
testcase_64 AC 3 ms
9,796 KB
testcase_65 AC 3 ms
9,796 KB
testcase_66 AC 3 ms
9,796 KB
testcase_67 AC 197 ms
70,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize(2)
#include<bits/stdc++.h>
// #define int long long
// #define uint unsigned long long
#define il inline
#define ct const
#define dl double
#define pk push_back
#define fi first
#define N 3010
#define se second
#define pii pair<int,int>
// #define inf (int)(1000000000000000000)
using namespace std;
#define lowbit(x) (x&-x)
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
il int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);ch=getchar();
	}
	return x*f;
}
char f__[40];
il void write(int x){
	int cnt=0;
	if(x<0){
		putchar('-');x=-x;
	}
	if(!x) putchar('0');
	while(x){
		f__[cnt++]=x%10+'0';x/=10;
	}
	while(cnt) putchar(f__[--cnt]);
}
int n,p[N],sum[N][N],f[N],g[N],s[N][N];
int stk[N],top,id[N];
signed main(){
    // freopen("1.in","r",stdin);
    // freopen("1.out","w",stdout);
	n=read();
	for(int i=1;i<=n;++i) p[i]=read();
	for(int i=1;i<=n;++i){
		for(int j=1;j<=i;++j) ++sum[i][p[j]];
		for(int j=1;j<=n;++j) sum[i][j]+=sum[i][j-1];
	}
	for(int i=0;i<=n;++i){
		for(int j=i+1;j<=n;++j){
			s[i][j]=s[i][j-1]+sum[i][n]-sum[i][p[j]];
		}
	}
	memset(f,0x3f,sizeof(f));
	memset(g,0x3f,sizeof(g));
	f[0]=0;
	for(int j=1;j<=n;++j){
		stk[top=1]=n;id[1]=0;
		for(int i=1;i<=n;++i){
			while(top&&stk[top]<i) --top;
			g[i]=f[id[top]]+s[id[top]][i];
			// cerr<<top<<" "<<id[top]<<" "<<g[i]<<" "<<stk[top]<<"\n";
			while(top){
				if(stk[top]<=i||f[id[top]]+s[id[top]][stk[top]]>=f[i]+s[i][stk[top]]){
					--top;continue;
				}
				int l=i+1,r=stk[top]-1;
				while(l<=r){
					// cerr<<"ERROR";
					int mid=(l+r)>>1;
					if(f[id[top]]+s[id[top]][mid]<=f[i]+s[i][mid]) r=mid-1;
					else l=mid+1;
				}
				--l;
				if(l>i){
					stk[++top]=l;id[top]=i;
				}
				break;
			}
			if(!top){
				stk[top=1]=n;id[top]=i;
			}
		}
		write(g[n]);putchar('\n');
		memcpy(f,g,sizeof(f));
	}
	return 0;
}
0