QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#411295#6627. Line TownJohnAlfnov3 48ms11984kbC++202.4kb2024-05-15 11:21:182024-05-15 11:21:19

Judging History

你现在查看的是最新测评结果

  • [2024-05-15 11:21:19]
  • 评测
  • 测评结果:3
  • 用时:48ms
  • 内存:11984kb
  • [2024-05-15 11:21:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int h[500005],hh[500005];
vector<int>g[500005][2];
long long f[500005][2];
long long qz[500005],hz[500005];
int A1[500005],A2[500005];
int B1[500005],B2[500005];
int by[500005],yb[500005];
int n,c[500005],a[500005];
void add(int x,int s){
	while(x<=n){
		c[x]+=s;
		x+=x&-x;
	}
}
int query(int x){
	int ans=0;
	while(x){
		ans+=c[x];
		x-=x&-x;
	}
	return ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&h[i]),h[i]*=(i%2?-1:1);
	int gs=0,tt=0;
	for(int i=1;i<=n;++i){
		if(!h[i])++gs,g[h[i]][0].emplace_back(i);
		else hh[++tt]=abs(h[i]);
	}
	sort(hh+1,hh+tt+1);
	tt=unique(hh+1,hh+tt+1)-hh-1;
	for(int i=1;i<=n;++i)if(h[i]){
		int w=lower_bound(hh+1,hh+tt+1,abs(h[i]))-hh;
		h[i]=(h[i]>0?1:-1)*w;
		g[abs(h[i])][h[i]>0].emplace_back(i);
	}
	memset(f,63,sizeof(f));
	f[tt+1][0]=0;
	int he=0;
	for(int i=tt;i>=1;--i){
		int A=g[i][0].size(),B=g[i][1].size();
		for(int j=0;j<2;++j){
			qz[0]=hz[A+B+1]=0;
			for(int k=1;k<=A+B;++k)qz[k]=hz[k]=2e18;
			int b1=0,b2=0;
			vector<int>v1,v2;
			A1[0]=B1[0]=A2[A+B+1]=B2[A+B+1]=0;
			for(int k=1;k<=A+B;++k){
				int x=0;
				A1[k]=A1[k-1],B1[k]=B1[k-1];
				if((j+k)%2==0){
					if(b1==A)break;
					x=g[i][0][b1++];++A1[k];
				}else{
					if(b2==B)break;
					x=g[i][1][b2++];++B1[k];
				}
				yb[k]=x;
				int gs=0;
				for(int I=1;I<x;++I)if(abs(h[I])<=i)++gs;
				for(auto cu:v1){
					if(cu<x)--gs;
					else ++gs;
				}
				v1.emplace_back(x);
				qz[k]=qz[k-1]+gs;
			}
			b1=A-1,b2=B-1;
			int jj=he-j;
			for(int k=A+B;k>=1;--k){
				A2[k]=A2[k+1],B2[k]=B2[k+1];
				int x=0;
				if((n-jj-(A+B-k))%2==1){
					if(b1<0)break;
					x=g[i][0][b1--];++A2[k];
				}else{
					if(b2<0)break;
					x=g[i][1][b2--];++B2[k];
				}
				by[k]=x;
				int gs=0;
				for(int I=x+1;I<=n;++I)if(abs(h[I])<i)++gs;
				for(auto cu:v2){
					if(cu<x)++gs;
				}
				v2.emplace_back(x);
				hz[k]=hz[k+1]+gs;
			}
			for(int k=0;k<=A+B;++k){
				if(A1[k]+A2[k+1]!=A)continue;
				if(B1[k]+B2[k+1]!=B)continue;
				for(int t=1;t<=n;++t)a[t]=(t<=k?yb[t]:by[t]),c[t]=0;
				int jb=qz[k]+hz[k+1];
				jb=0;
				for(int t=1;t<=n;++t){
					jb+=t-1-query(a[t]-1);
					add(a[t],1);
				}
				f[i][j^(k&1)]=min(f[i][j^(k&1)],f[i+1][j]+jb);
			}
		}
		he+=A+B;
	}
	long long ans=min(f[1][0],f[1][1]);
	if(ans>1e18)puts("-1");
	else printf("%lld\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 0ms
memory: 11440kb

input:

10
1 1 1 1 1 -1 -1 -1 1 -1

output:

-1

result:

ok 1 number(s): "-1"

Test #2:

score: 3
Accepted
time: 0ms
memory: 11604kb

input:

10
1 1 1 1 1 1 -1 1 1 -1

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 3
Accepted
time: 2ms
memory: 11848kb

input:

1
-1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 3
Accepted
time: 29ms
memory: 11980kb

input:

2000
1 -1 -1 -1 -1 -1 1 -1 1 1 1 -1 -1 -1 1 1 -1 1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 1 1 1 -1 1 1 -1 -1 1 -1 1 1 1 1 -1 -1 -1 1 1 -1 -1 1 1 1 -1 -1 -1 -1 1 -1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 -1 -1 ...

output:

15146

result:

ok 1 number(s): "15146"

Test #5:

score: 3
Accepted
time: 29ms
memory: 11956kb

input:

2000
-1 -1 1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 1 -1 1 1 -1 1 1 1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 -1 -1 -1 1 -1 -1 1 1 1 1 -1 -1 1 -1 -1 1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 -1 1 1 -1 1 1 1 -1 1 1 -1 -1 -1 ...

output:

24933

result:

ok 1 number(s): "24933"

Test #6:

score: 3
Accepted
time: 45ms
memory: 11736kb

input:

2000
1 1 -1 -1 -1 -1 1 1 -1 1 -1 1 1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 1 1 -1 1 1 -1 1 1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 1 -1 -1 -1 -1 1 -1 1 -1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 -1 1 1 1 -1 1 1 1 1 1 -1 -1 -1 1 1 -1 1 1 -1 -1 1 -1 1 -1 -1 1 1 1 -1 -1 1 1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 1 -1 -1 1 -...

output:

18090

result:

ok 1 number(s): "18090"

Test #7:

score: 3
Accepted
time: 7ms
memory: 11516kb

input:

2000
-1 -1 -1 -1 -1 1 1 -1 -1 -1 1 1 -1 -1 1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 -1 1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 1 1 -1 -1 -1 -1 1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 -1 -1 1 -1 -1 1 1 1 -1 1 1 -1 -1 -1 1 -1 -1 1 -1 1 1 1 -1 1 -1 -1 1 1 1...

output:

-1

result:

ok 1 number(s): "-1"

Test #8:

score: 3
Accepted
time: 46ms
memory: 11984kb

input:

2000
-1 1 -1 1 1 -1 -1 1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 1 1 -1 -1 -1 1 -1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 -1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1 -1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 1 1 -1 -1 -1 1 -1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 -1 -1 -1 ...

output:

9973

result:

ok 1 number(s): "9973"

Test #9:

score: 3
Accepted
time: 45ms
memory: 11704kb

input:

2000
-1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 ...

output:

499500

result:

ok 1 number(s): "499500"

Test #10:

score: 3
Accepted
time: 42ms
memory: 11684kb

input:

2000
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...

output:

499500

result:

ok 1 number(s): "499500"

Test #11:

score: 3
Accepted
time: 41ms
memory: 11680kb

input:

1999
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...

output:

499500

result:

ok 1 number(s): "499500"

Test #12:

score: 3
Accepted
time: 48ms
memory: 11720kb

input:

1997
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...

output:

498501

result:

ok 1 number(s): "498501"

Test #13:

score: 3
Accepted
time: 33ms
memory: 11848kb

input:

2000
1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...

output:

-1

result:

ok 1 number(s): "-1"

Subtask #2:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #14:

score: 3
Accepted
time: 0ms
memory: 11588kb

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #15:

score: 0
Time Limit Exceeded

input:

500000
1 -1 -1 -1 -1 1 -1 -1 1 -1 1 -1 -1 -1 -1 1 -1 1 1 -1 1 -1 1 1 1 -1 -1 1 -1 1 1 1 1 1 -1 -1 -1 1 -1 1 1 1 1 1 1 1 -1 -1 1 1 -1 1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 1 -1 -1 -1 1 -1 1 1 -1 1 1 1 -1 1 1 1 1 1 1 1 1 1 -1 1 1 1 -1 -1 -1 1 -1 1 -1 1 1 1 1 -1 1 -1 -1 1 1 -1 1 1 1 1 -1 1 1 -1 -1 1 ...

output:


result:


Subtask #3:

score: 0
Runtime Error

Dependency #1:

100%
Accepted

Test #25:

score: 0
Runtime Error

input:

10
0 1 0 0 1 1 -1 0 1 -1

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%

Subtask #5:

score: 0
Runtime Error

Test #60:

score: 0
Runtime Error

input:

10
3 10 5 -9 7 2 -6 1 8 0

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%