QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#119831#6627. Line Townsjcx0 5ms26012kbC++143.2kb2023-07-05 21:33:122023-07-05 21:33:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-05 21:33:14]
  • 评测
  • 测评结果:0
  • 用时:5ms
  • 内存:26012kb
  • [2023-07-05 21:33:12]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define re int
#define ll long long
inline int read(){
	int x=0,ff=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')ff=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^'0');c=getchar();}
	return x*ff; 
}
const ll inf=1e18;
int n,a[500005],c[500005],N;
vector<int> d[500005];
ll f[2],g[2];
inline int fs(int x){return x&-x;}
int s[500005];
inline void add(int x,int y){while(x<=n)s[x]+=y,x+=fs(x);}
inline int find(int x){int y=0;while(x)y+=s[x],x-=fs(x);return y;}
int p[500005],q[500005];
bool qwq[500005];
int _s[500005],kk[500005];
inline void _add(int x,int y){while(x<=n)_s[x]+=y,x+=fs(x);}
inline int _find(int x){int y=0;while(x)y+=_s[x],x-=fs(x);return y;}
void solve(int m,bool fl){
	g[0]=f[0];g[1]=f[1];
	f[0]=f[1]=inf;int x,t;ll y;
	for(re i=1;i<=m;i++)qwq[i]=(q[i]>0)^(p[i]&1);
//	if(fl){
		for(re l=0;l<2;l++){y=0;
			if(g[l]==inf)continue;
			x=l^1;vector<int> V[2],U,K;
			int v[2],u[2];v[0]=v[1]=0;
			for(re i=1;i<=m;i++)V[qwq[i]].emplace_back(i);
			while(23333){
				if(v[x]==V[x].size())break;
				U.emplace_back(V[x][v[x]]);v[x]++;
				x^=1;
			}
			u[0]=V[0].size()-1;u[1]=V[1].size()-1;
			t=x;x=l^fl^1;
			while(23333){
				if(u[x]<v[x])break;
				K.emplace_back(V[x][u[x]]);u[x]--;
				x^=1;
			}
			if(K.size()+U.size()!=V[0].size()+V[1].size())continue;
			int now=0;
			for(re i:U)y+=(kk[now++]=_find(n)-_find(p[i])+find(p[i])),_add(p[i],1);
			for(re i:K)y+=(kk[now++]=_find(n)-_find(p[i])+find(n)-find(p[i])),_add(p[i],1);
			for(re i:U)_add(p[i],-1);
			f[t^1]=min(f[t^1],y+g[l]);
			for(re i=U.size()-1;i>=0;i--){
				if(qwq[U[i]]!=x){
					if(i>0){
						i--;
						y-=kk[i];y+=_find(p[U[i]])+find(n)-find(p[U[i]]);_add(p[U[i]],1);
						x^=1;t^=1;i++;
						y-=kk[i];y+=_find(p[U[i]])+find(n)-find(p[U[i]]);_add(p[U[i]],1);
						x^=1;t^=1;i--;f[t^1]=min(f[t^1],y+g[l]);i--;
					}
					else {
						y-=kk[i];y+=_find(p[U[i]])+find(n)-find(p[U[i]]);_add(p[U[i]],1);
						x^=1;t^=1;i--;
					}
					continue;
				}
				y-=kk[i];y+=_find(p[U[i]])+find(n)-find(p[U[i]]);_add(p[U[i]],1);
				x^=1;t^=1;f[t^1]=min(f[t^1],y+g[l]);
			}
			for(re i:U)_add(p[i],-1);
			for(re i:K)add(p[i],-1);
		}
//	}
//	else {
//		for(re l=0;l<2;l++){y=0;
//			for(re i=1;i<=m;i++){
//				tuu[i]=tuu[i-1];
//				if(((l^(i&1))^qwq[i])!=1)tuu[i]=1;
//				y+=find(p[i]);
//			}x=l;
//			for(re i=m;i>=0;i--){
//				if(!tuu[i])f[l^(i&1)]=min(f[l^(i&1)],g[l]+y);
//				if((qwq[i]^x)!=0)break;
//				x^=1;y-=(find(p[i])<<1)+find(n);
//			}
//		}
//	}
}
int main(){
	n=read();
	for(re i=1;i<=n;i++)a[i]=read(),c[++N]=abs(a[i]);
	sort(c+1,c+N+1);N=unique(c+1,c+N+1)-c-1;
	for(re i=1;i<=n;i++){
		d[lower_bound(c+1,c+N+1,abs(a[i]))-c].emplace_back(i);
		add(i,1);
	}
	bool fl=n&1;int x;
	f[0]=0;f[1]=inf;
	for(re i=N;i;i--){x=0;
		if(c[i]==0)break;
		for(re j:d[i]){
			add(j,-1);p[++x]=j;q[x]=a[j];
		}
		solve(x,fl);fl^=(x&1);
		//cerr<<f[0]<<" "<<f[1]<<endl;
	}
	if(min(f[0],f[1])>=inf)puts("-1");
	else cout<<min(f[0],f[1]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 3ms
memory: 26012kb

input:

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

output:

-1

result:

ok 1 number(s): "-1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 25916kb

input:

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

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 5ms
memory: 25960kb

input:

1
-1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: -3
Wrong Answer
time: 4ms
memory: 23984kb

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:

14921

result:

wrong answer 1st numbers differ - expected: '15146', found: '14921'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Wrong Answer

Test #60:

score: 4
Accepted
time: 1ms
memory: 25912kb

input:

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

output:

-1

result:

ok 1 number(s): "-1"

Test #61:

score: -4
Wrong Answer
time: 0ms
memory: 25924kb

input:

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

output:

7

result:

wrong answer 1st numbers differ - expected: '13', found: '7'

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%