QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#645810#6627. Line TownProsperity0 0ms28444kbC++142.0kb2024-10-16 19:56:172024-10-16 19:56:21

Judging History

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

  • [2024-10-16 19:56:21]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:28444kb
  • [2024-10-16 19:56:17]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

int n,m;

const int N=500010;
int a[N],b[N];
long long f[N][2][2];
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
struct BIT{
	int a[N];
	int lowbit(int x){return x&-x;}
	void clear(){for(int i=1;i<=m;++i)a[i]=0;}
	void add(int x,int v){for(;x<=m;x+=lowbit(x))a[x]+=v;}
	int ask(int x){int res=0;for(;x;x-=lowbit(x))res+=a[x];return res;}
}fwk;
int pr[N],sf[N];
int c[N];
int main(){
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i],b[i]=abs(a[i]);
	sort(b+1,b+n+1);
	m=unique(b+1,b+n+1)-b-1;
	// for(int i=1;i<=m;++i)printf("%d ",b[i]);puts("");
	for(int i=1;i<=n;++i){
		if(a[i]>=0)a[i]=lower_bound(b+1,b+m+1,a[i])-b;
		else a[i]=-(lower_bound(b+1,b+m+1,-a[i])-b);
		if(i&1)a[i]=-a[i];
		c[abs(a[i])]=i;
		// printf("%d ",a[i]);
	}
	// puts("");
	for(int i=1;i<=n;++i){
		pr[i]=fwk.ask(abs(a[i])-1);
		fwk.add(abs(a[i]),1);
	}
	fwk.clear();
	for(int i=n;i>=1;--i){
		sf[i]=fwk.ask(abs(a[i])-1);
		fwk.add(abs(a[i]),1);
		// swap(sf[i],pr[i]);
	}
	// for(int i=1;i<=n;++i)printf("%d ",sf[i]);puts("");
	// for(int i=1;i<=n;++i)printf("%d ",pr[i]);puts("");
	memset(f,0x3f,sizeof f);
	f[0][1][1]=f[0][0][0]=0;
	for(int i=0;i<=n;++i){
		if(i==0){
			
		// printf("%d:%d %d %d %d\n",i,f[i][0][0],f[i][0][1],
			// f[i][1][0],f[i][1][1]);
			continue;
		}
		if(a[c[i]]>=0){
			f[i][1][1]=min(f[i-1][1][0]+sf[c[i]],f[i-1][0][1]+pr[c[i]]);
			f[i][0][1]=f[i-1][0][0]+sf[c[i]];
			f[i][1][0]=f[i-1][0][0]+pr[c[i]];
			// f[i][0][0]
		}else{
			f[i][0][0]=min(f[i-1][0][1]+sf[c[i]],f[i-1][1][0]+pr[c[i]]);
			f[i][1][0]=f[i-1][1][1]+sf[c[i]];
			f[i][0][1]=f[i-1][1][1]+pr[c[i]];
		}
		// printf("%d:%d %d %d %d\n",i,f[i][0][0],f[i][0][1],
			// f[i][1][0],f[i][1][1]);
	}
	ll ans=min(f[n][0][0],f[n][0][1]);
	ll anss=0;
	for(int i=1;i<=n;++i){
		if(i%2==1&&a[c[i]]>0||i%2==0&&a[c[i]]<0){
			anss=INF;
			break;
		}
		anss+=sf[c[i]];
	}
	ans=min(ans,anss);
	printf("%lld\n",ans==INF?-1:ans);
	return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 28396kb

input:

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

output:

0

result:

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

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: 0ms
memory: 26288kb

input:

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

output:

-1

result:

ok 1 number(s): "-1"

Test #61:

score: 0
Wrong Answer
time: 0ms
memory: 28444kb

input:

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

output:

-1

result:

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

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%