QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#118571#6627. Line Townyyyyxh0 6ms60392kbC++173.4kb2023-07-03 17:27:582023-07-03 17:28:01

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-03 17:28:01]
  • 评测
  • 测评结果:0
  • 用时:6ms
  • 内存:60392kb
  • [2023-07-03 17:27:58]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int read(){
	char c=getchar();int x=0;bool f=0;
	while(c<48||c>57) f|=(c=='-'),c=getchar();
	do x=(x<<1)+(x<<3)+(c^48),c=getchar();
	while(c>=48&&c<=57);
	if(f) return -x;
	return x;
}
typedef long long ll;
const int N=1000003;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,a[N];
ll f[2][2][2],g[2][2][2];
void chmn(ll &x,ll v){if(x>v) x=v;}
struct bit{
	int tr[N],stk[N],tp;
	int qry(int x,bool t){
		int res=0;
		for(int i=x;i;i^=(i&-i)) res+=tr[i];
		if(t) return tp-res;
		return res;
	}
	void upd(int x){
		stk[++tp]=x;
		for(int i=x;i<=n;i+=(i&-i)) ++tr[i];
	}
	void clear(){
		while(tp){
			for(int i=stk[tp--];i<=n;i+=(i&-i))
				if(tr[i]) tr[i]=0;
				else break;
		}
	}
}F,G;
int buc[N],rk;
vector<int> v[N][2];
int seq[N];
int main(){
	n=read();
	for(int i=1;i<=n;++i) a[i]=read();
	for(int i=1;i<=n;++i) if(i&1) a[i]=-a[i];
	for(int i=1;i<=n;++i) buc[++rk]=abs(a[i]);
	sort(buc+1,buc+rk+1);rk=unique(buc+1,buc+rk+1)-buc-1;
	for(int i=1;i<=n;++i)
		if(a[i]<0) v[lower_bound(buc+1,buc+rk+1,-a[i])-buc][1].emplace_back(i);
		else v[lower_bound(buc+1,buc+rk+1,a[i])-buc][0].emplace_back(i);
	for(int t=0;t<2;++t)
		for(int x=0;x<2;++x)
			for(int y=0;y<2;++y) f[t][x][y]=INF;
	if(buc[1]) f[1][0][0]=f[0][1][1]=0;
	else{
		int len=v[1][0].size();
		for(int x:v[1][0]) F.upd(x);
		for(int i=0;i<=len;++i){
			f[1][i&1][(len^i)&1]=0;
			f[0][~i&1][~(len^i)&1]=0;
		}
	}
	for(int pos=1;pos<=rk;++pos){
		if(!buc[pos]) continue;
		int len=v[pos][0].size()+v[pos][1].size();
		for(int t=0;t<2;++t)
			for(int x=0;x<2;++x)
				for(int y=0;y<2;++y)
					g[t][x][y]=f[t][x][y],f[t][x][y]=INF;
		for(int t=0;t<2;++t)
			for(int x=0;x<2;++x)
				for(int y=0;y<2;++y){
					if(g[t][x][y]==INF) continue;
					if(x!=y){
						int o[2]={0,0};
						ll cur=0;
						for(int i=0,par=y;i<len;++i,par^=1){
							if(o[par]>=int(v[pos][par].size())) goto nxt;
							seq[i]=v[pos][par][o[par]++];
							cur+=F.qry(seq[i],1)+G.qry(seq[i],1);
							G.upd(seq[i]);
						}
						chmn(f[t][x][y^(len&1)],g[t][x][y]+cur);
						for(int i=1;i<=len;++i){
							cur-=F.qry(seq[i-1],1);
							cur+=F.qry(seq[i-1],0);
							chmn(f[t^(i&1)][x^(i&1)][y^((i^len)&1)],g[t][x][y]+cur);
						}
						nxt: G.clear();
					}
					else{
						for(int tt=0;tt<2;++tt){
							int o[2]={0,0};
							ll cur=0;
							if(tt){
								if(v[pos][x].empty()) continue;
								seq[0]=v[pos][x][o[x]++];
								cur+=F.qry(seq[0],0);
								G.upd(seq[0]);
							}
							for(int i=tt,par=y;i<len;++i,par^=1){
								if(o[par]>=int(v[pos][par].size())) goto jump;
								seq[i]=v[pos][par][o[par]++];
								cur+=F.qry(seq[i],1)+G.qry(seq[i],1);
								G.upd(seq[i]);
							}
							chmn(f[t^tt][x^tt][y^(len&1)^tt],g[t][x][y]+cur);
							for(int i=tt;i+1<len;i+=2){
								if(seq[i+1]>seq[i+2]) --cur;else ++cur;
								swap(seq[i+1],seq[i+2]);
								cur-=F.qry(seq[i+1],1);
								cur-=F.qry(seq[i+2],1);
								cur+=F.qry(seq[i+1],0);
								cur+=F.qry(seq[i+2],0);
								chmn(f[t^tt][x^tt][y^(len&1)^tt],g[t][x][y]+cur);
							}
							jump: G.clear();
						}
					}
				}
		for(int x:v[pos][0]) F.upd(x);
		for(int x:v[pos][1]) F.upd(x);
	}
	ll res=INF;
	for(int x=0;x<2;++x)
		for(int y=0;y<2;++y) chmn(res,f[0][x][y]);
	if(res==INF) puts("-1");
	else printf("%lld\n",res);
	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: 5ms
memory: 57900kb

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: 58284kb

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: 1ms
memory: 58400kb

input:

1
-1

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: -3
Wrong Answer
time: 3ms
memory: 58372kb

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:

15679

result:

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

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: 0
Wrong Answer
time: 6ms
memory: 60392kb

input:

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

output:

22

result:

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

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%