QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#113773#6627. Line Town1kri#0 3ms32132kbC++142.4kb2023-06-19 11:26:232024-05-31 14:07:16

Judging History

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

  • [2024-05-31 14:07:16]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:32132kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-19 11:26:23]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define inf 1000000000000ll
using namespace std;
int n;
int _abs(int x){
	if (x<0)return -x;
	return x;
}
struct node{
	int p,v;
	bool operator <(const node &y)const{
		if (_abs(v)!=_abs(y.v))return _abs(v)<_abs(y.v);
		if (v!=y.v)return v>y.v;
		return p<y.p;
	}
}a[1000005];
struct BIT{
	int tree[1000005];
	void clear(){
		memset(tree,0,sizeof(tree));
		return;
	}
	void add(int p,int v){
		while(p<=n)tree[p]+=v,p+=(p&(-p));
		return;
	}
	int ask(int p){
		int ans=0;
		while(p)ans+=tree[p],p-=(p&(-p));
		return ans;
	}
}bit;
ll c[1000005][2];
ll f[1000005][2];
int l0,t0[1000005],l1,t1[1000005];
int pos[1000005];
ll ask(int len){
	ll ans=0;
	for (int i=1;i<=len;i++)
		if (pos[i]==0)return inf;
	for (int i=1;i<=len;i++){
		ans+=bit.ask(n)-bit.ask(pos[i]);
		bit.add(pos[i],1);
	}
	for (int i=1;i<=len;i++)bit.add(pos[i],-1);
	return ans;
}
int get_o(int o,int x){
	return ((x-1)&1)^o;
}
ll dfs(int now,int o){
	if (f[now][o]!=-1)return f[now][o];
	if (now==0)return 0;
	int l=now,r=now;
	while(l>1&&_abs(a[l-1].v)==_abs(a[r].v))l--;
	l0=l1=0;
	for (int i=l;i<=r;i++)
		if (a[i].v<0)t0[++l0]=a[i].p;
		else t1[++l1]=a[i].p;
	int len=r-l+1;
	int cnt0=0,cnt1=0;
	ll s=0;
	for (int i=l0+1;i<=len;i++)t0[i]=0;
	for (int i=l1+1;i<=len;i++)t1[i]=0;
	for (int i=1;i<=len;i++)
		if (get_o(o,i)==1){
			cnt0++;
			pos[i]=t0[cnt0];
			s+=c[t0[cnt0]][0];
		}
		else{
			cnt1++;
			pos[i]=t1[cnt1];
			s+=c[t1[cnt1]][0];
		}
	ll ans=inf;
	if (cnt0==l0&&cnt1==l1)ans=min(ans,s+ask(len));
	for (int i=len;i>=1;i--){
		if (get_o(o,i)==1){
			cnt0--;
			s-=c[t0[cnt0]][0];
		}
		else{
			cnt1--;
			s-=c[t1[cnt1]][0];
		}
		if (get_o(o,now-(len-i))==0){
			cnt0++;
			pos[i]=t0[cnt0];
			s+=c[t0[cnt0]][1];
		}
		else{
			cnt1++;
			pos[i]=t1[cnt1];
			s+=c[t1[cnt1]][1];
		}
		if (cnt0==l0&&cnt1==l1)ans=min(ans,s+ask(len));
	}
	return f[now][o]=ans;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		a[i].p=i;
		scanf("%d",&a[i].v);
		if (i&1)a[i].v=-a[i].v;
	}
	sort(a+1,a+1+n);
	int j=1;
	for (int i=1;i<=n;i++){
		while(_abs(a[j].v)<_abs(a[i].v))bit.add(a[j].p,1),j++;
		c[i][0]=bit.ask(a[i].p-1);
		c[i][1]=bit.ask(n)-bit.ask(a[i].p);
	}
	c[0][0]=c[0][1]=inf;
	bit.clear();
	memset(f,-1,sizeof(f));
	ll ans=dfs(n,0);
	if (ans==inf)ans=-1;
	cout<<ans<<endl;
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

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

input:

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

output:

-1

result:

ok 1 number(s): "-1"

Test #2:

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

input:

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

output:

4

result:

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

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

input:

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

output:

-999999999999

result:

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

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%