QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411274 | #6627. Line Town | JohnAlfnov | 0 | 5ms | 28024kb | C++20 | 2.0kb | 2024-05-15 11:01:56 | 2024-05-15 11:01:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int h[500005],hh[500005];
vector<int>g[500005][2];
long long f[500005][2];
long long qz[500005],hz[500005];
int A1[500005],A2[500005];
int B1[500005],B2[500005];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",&h[i]),h[i]*=(i%2?-1:1);
int gs=0,tt=0;
for(int i=1;i<=n;++i){
if(!h[i])++gs,g[h[i]][0].emplace_back(i);
else hh[++tt]=abs(h[i]);
}
sort(hh+1,hh+tt+1);
tt=unique(hh+1,hh+tt+1)-hh-1;
for(int i=1;i<=n;++i)if(h[i]){
int w=lower_bound(hh+1,hh+tt+1,abs(h[i]))-hh;
h[i]=(h[i]>0?1:-1)*w;
g[abs(h[i])][h[i]>0].emplace_back(i);
}
memset(f,63,sizeof(f));
f[tt+1][0]=0;
int he=0;
for(int i=tt;i>=1;--i){
int A=g[i][0].size(),B=g[i][1].size();
for(int j=0;j<2;++j){
qz[0]=hz[A+B+1]=0;
for(int k=1;k<=A+B;++k)qz[k]=hz[k]=2e18;
int b1=0,b2=0;
vector<int>v1,v2;
A1[0]=B1[0]=A2[A+B+1]=B2[A+B+1]=0;
for(int k=1;k<=A+B;++k){
int x=0;
A1[k]=A1[k-1],B1[k]=B1[k-1];
if((j+k)%2==0){
if(b1==A)break;
x=g[i][0][b1++];++A1[k];
}else{
if(b2==B)break;
x=g[i][1][b2++];++A2[k];
}
int gs=0;
for(int I=1;I<x;++I)if(abs(h[I])<=i)++gs;
for(auto cu:v1){
if(cu<x)--gs;
else ++gs;
}
v1.emplace_back(x);
qz[k]=qz[k-1]+gs;
}
b1=A-1,b2=B-1;
int jj=he-j;
for(int k=A+B;k>=1;--k){
A2[k]=A2[k+1],B2[k]=B2[k+1];
int x=0;
if((n-jj-(A+B-k))%2==1){
if(b1<0)break;
x=g[i][0][b1--];++A2[k];
}else{
if(b2<0)break;
x=g[i][1][b2--];++B2[k];
}
int gs=0;
for(int I=x+1;I<=n;++I)if(abs(h[I])<i)++gs;
for(auto cu:v2){
if(cu<x)++gs;
}
v2.emplace_back(x);
hz[k]=hz[k+1]+gs;
}
for(int k=0;k<=A+B;++k){
if(A1[k]+A2[k+1]!=A)continue;
if(B1[k]+B2[k+1]!=B)continue;
f[i][j^(k&1)]=min(f[i][j^(k&1)],f[i+1][j]+qz[k]+hz[k+1]);
}
}
he+=A+B;
}
long long ans=min(f[1][0],f[1][1]);
if(ans>1e18)puts("-1");
else printf("%lld\n",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: 3ms
memory: 27336kb
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: 5ms
memory: 28024kb
input:
10 1 1 1 1 1 1 -1 1 1 -1
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: -3
Wrong Answer
time: 4ms
memory: 25360kb
input:
1 -1
output:
-1
result:
wrong answer 1st numbers differ - expected: '0', found: '-1'
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: 27708kb
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: 3ms
memory: 25928kb
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%