QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#305742#6627. Line Towncqbzxzj0 2ms14348kbC++143.0kb2024-01-15 22:28:482024-01-15 22:28:49

Judging History

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

  • [2024-01-15 22:28:49]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:14348kb
  • [2024-01-15 22:28:48]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int SZ=1<<20;
char buf[SZ],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,SZ,stdin),p1==p2)?EOF:*p1++)
ll read(){
	ll n=0; bool m=0;
	char c=gc();
	while(c<'0'||c>'9')m=c=='-',c=gc();
	while(c>='0'&&c<='9')n=(n<<3)+(n<<1)+(c^'0'),c=gc();
	return m?-n:n;
}
const int N=500005;
const ll inf=0x3f3f3f3f3f3f3f3f;
int n,p[N],tp,q[N],tq,cur;
ll f[2][N],pre[N],suf[N],sp[N],sq[N];
struct node{int x,i;}a[N];
bool cmp(node x,node y){return abs(x.x)<abs(y.x)||abs(x.x)==abs(y.x)&&x.i<y.i;}
int b[N];
void upd(int x,int y){
	while(x<=n)b[x]+=y,x+=x&-x;
}
int ask(int x){
	int y=0;
	while(x)y+=b[x],x-=x&-x;
	return y;
}
void solve(int r,int X){
	// for(int i=1;i<=tp;i++)printf("%d ",p[i]);
	// printf("p\n");
	// for(int i=1;i<=tq;i++)printf("%d ",q[i]);
	// printf("q\n");
	int i=0,j=0,k=0,op=1,pi=0,pj=0;
	while(1){
		k++;
		if(op){
			if(i==tp)break;
			i++;
			while(pj<j&&q[pj+1]<p[i])pj++;
			pre[k]=pre[k-1]+(p[i]-1)-(i-1)-pj;
		}else{
			if(j==tq)break;
			j++;
			while(pi<i&&p[pi+1]<q[j])pi++;
			pre[k]=pre[k-1]+(q[j]-1)-(j-1)-pi;
		}
		op^=1;
	}
	i=tp+1,j=tq+1,k=tp+tq+1,op=!(r&1),pi=tp+1,pj=tq+1,suf[k]=0;
	while(1){
		k--;
		if(op){
			if(i==1)break;
			i--;
			while(pj>j&&q[pj-1]>p[i])pj--;
			suf[k]=suf[k+1]+(r-p[i])-(tp-i)-(tq+1-pj);
		}else{
			if(j==1)break;
			j--;
			while(pi>i&&p[pi-1]>q[j])pi--;
			suf[k]=suf[k+1]+(r-q[j])-(tq-j)-(tp+1-pi);
		}
		op^=1;
	}
	for(i=1,j=0;i<=tp;i++){
		while(j<tq&&q[j+1]<p[i])j++;
		sp[i]=sp[i-1]+j;
	}
	for(j=1,i=0;j<=tq;j++){
		while(i<tp&&p[i+1]<q[j])i++;
		sq[j]=sq[j-1]+i;
	}
	i=0,j=0,k=0,op=1,pi=0,pj=0;
	int rp=tp-i,rq=tq-j;
	if(rp==rq||(r&1)&&rp+1==rq||!(r&1)&&rp==rq+1){
		ll x=pre[k]+suf[k+1];
		if(p[i]<q[j])x-=sq[j]-sq[pj];
		else x-=sp[i]-sp[pi];
		// printf(">>%d %lld\n",k,x);
		f[X][cur]=min(f[X][cur],f[X^(k&1)][cur-1]+x);
	}
	while(1){
		k++;
		if(op){
			if(i==tp)break;
			i++;
			while(pj<tq&&q[pj+1]<p[i])pj++;
		}else{
			if(j==tq)break;
			j++;
			while(pi<tp&&p[pi+1]<q[j])pi++;
		}
		rp=tp-i,rq=tq-j;
		if(rp==rq||(r&1)&&rp+1==rq||!(r&1)&&rp==rq+1){
			ll x=pre[k]+suf[k+1];
			if(p[i]<q[j])x-=sq[j]-sq[pj];
			else x-=sp[i]-sp[pi];
			// printf(">>%d %lld\n",k,x);
			f[X][cur]=min(f[X][cur],f[X^(k&1)][cur-1]+x);
		}
		op^=1;
	}
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		a[i].x=read(),a[i].i=i;
		if(i&1)a[i].x=-a[i].x;
	}
	stable_sort(a+1,a+n+1,cmp);
	for(int l=1,r=1;l<=n;l=r=r+1){
		while(r<n&&abs(a[l].x)==abs(a[r+1].x))r++;
		for(int i=l;i<=r;i++)upd(a[i].i,1);
		if(a[l].x==0)continue;
		cur++,f[0][cur]=f[1][cur]=inf;
		tp=tq=0;
		for(int i=l;i<=r;i++){
			if(a[i].x>0)p[++tp]=ask(a[i].i);
			else q[++tq]=ask(a[i].i);
		}
		solve(r,0);
		tp=tq=0;
		for(int i=l;i<=r;i++){
			if(a[i].x<0)p[++tp]=ask(a[i].i);
			else q[++tq]=ask(a[i].i);
		}
		solve(r,1);
		// printf("%lld %lld\n",f[0][cur],f[1][cur]);
	}
	ll ans=min(f[0][cur],f[1][cur]);
	printf("%lld",ans==inf?-1:ans);
	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: 0ms
memory: 11956kb

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: 2ms
memory: 13992kb

input:

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

output:

-7

result:

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

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: 2ms
memory: 13996kb

input:

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

output:

-1

result:

ok 1 number(s): "-1"

Test #61:

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

input:

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

output:

13

result:

ok 1 number(s): "13"

Test #62:

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

input:

2000
40667 -598150 -1084780 1201651 1570514 -1859539 -2029075 2941581 -2945945 3038404 3447919 5293872 -5335692 -5669647 5973784 6041345 6346915 -7222112 8820986 -9153143 9563103 9749206 -9894732 -11847193 11987150 12161864 13336572 13528051 -13722732 -13836176 -15497141 -15841563 15862227 16618123 ...

output:

-1

result:

ok 1 number(s): "-1"

Test #63:

score: 0
Accepted
time: 2ms
memory: 14044kb

input:

2000
3038404 -798315545 693574695 172661079 516504064 164016456 193562146 -131746730 382134316 -398886978 188767854 -834289064 -965673210 -826409444 -281381674 450991903 -592752625 81651101 -594873306 -352059270 -651772982 540062674 -769881300 68999588 307151563 -129950325 550154987 354801227 840540...

output:

658039

result:

ok 1 number(s): "658039"

Test #64:

score: 0
Accepted
time: 1ms
memory: 11968kb

input:

2000
-1095 -925 -1049 -1519 951 -1673 -776 345 -38 -1735 -276 -1730 123 -1629 -1896 -1576 -1115 1145 15 797 -948 287 1487 1195 1269 -1240 -1571 -275 -1915 -369 -1221 -1590 -1392 -100 1688 -1287 -241 1130 -1375 -965 669 -147 -307 -795 -1207 1939 120 -305 -915 -1078 -1448 1458 -603 1935 658 774 1471 7...

output:

668545

result:

ok 1 number(s): "668545"

Test #65:

score: 0
Accepted
time: 2ms
memory: 14096kb

input:

2000
1290 1487 -1947 -255 457 -1202 1313 36 -1511 898 1739 987 1809 -1986 -1015 -1127 -703 -223 179 557 199 349 1099 -259 -1401 -1244 -1116 646 -295 1713 1512 127 -1660 343 -1921 -1326 -549 831 1963 -1743 1655 -698 1792 366 1517 -51 404 -1853 -1295 1652 -130 -1562 -1850 -582 1504 1888 822 -24 1807 9...

output:

663841

result:

ok 1 number(s): "663841"

Test #66:

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

input:

2000
56 -1667 -1636 -671 -1311 348 976 1381 -710 -477 -1301 756 -510 495 -1215 -278 1134 950 59 1739 -33 -839 -862 605 761 827 -1708 -1180 -607 1624 -120 -1198 624 -1237 -1874 1788 1005 -331 1266 -467 -1213 1736 -182 594 775 1209 -832 300 1188 -994 -191 -217 1360 -1907 71 436 1294 -590 913 -747 -629...

output:

667052

result:

ok 1 number(s): "667052"

Test #67:

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

input:

1999
-758656 -113741 -374719 7680 -227905 -201318 -200890 -84484 777096 -167712 -126972 -244117 835074 161027 923025 -224756 973701 36622 -913757 -920737 -976062 461264 147694 -162457 358437 -308202 385370 808271 -523703 -303454 -522131 -664739 -505124 306509 948216 948694 -467953 -768055 769796 486...

output:

675957

result:

ok 1 number(s): "675957"

Test #68:

score: 0
Accepted
time: 1ms
memory: 6052kb

input:

1
0

output:

0

result:

ok 1 number(s): "0"

Test #69:

score: -4
Wrong Answer
time: 1ms
memory: 14040kb

input:

2
1000000000 999999999

output:

0

result:

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

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%