QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#119837 | #6627. Line Town | WhaleAtCola | 0 | 3ms | 33276kb | C++14 | 2.5kb | 2023-07-05 21:43:35 | 2023-07-05 21:43:37 |
Judging History
answer
#include<bits/stdc++.h>
#define re register
#define debug(...) fprintf(stderr,__VA_ARGS__);
using namespace std;
typedef long long ll;
typedef double db;
inline ll win(){
ll x=0;bool w=0;char c=getchar();
while(c<'0'||c>'9') w|=c=='-',c=getchar();
while(c<='9'&&c>='0') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return w?-x:x;
}
const int N=505050;
const ll inf=0x3f3f3f3f3f3f3f3fll;
struct bit{
int f[N],res,n;
inline void add(int x,int v){res+=v;for(;x<=n;x+=(x&-x)) f[x]+=v;}
inline int q0(int x){int v=0;for(;x;x^=(x&-x)) v+=f[x];return v;}
inline int q1(int x){return res-q0(x);}
}b1,b2;
int c[N];
inline void tran(int k,ll &ret,const vector<int> &p1,const vector<int> &p0,ll *r,int l){
int s1=(int)p1.size()-1,s0=(int)p0.size()-1,t=0;
ll res=0;
if(abs(s1-s0)>1) return ;
if((s0==s1+1&&!l)||(s1==s0+1&&l)) return ;
for(re int i=0;i<=min(s0,s1);++i) c[++t]=p0[i],c[++t]=p1[i];
for(re int i=1;i<=t;++i) res+=b1.q0(c[i])+b2.q1(c[i]),b2.add(c[i],1);
// debug("tran:%d %d %d %lld\n",k,s1,s0,res);
// for(re int i=1;i<=t;++i) debug("%d%c",c[i]," \n"[i==t]);
if(l){
if(s0==s1+1) res+=b1.q1(p0[s0])+b2.q1(p0[s0]);
for(re int i=t;i>=1;--i,k^=1){
ret=min(ret,res+r[k]);
res+=b1.q1(c[i])-b1.q0(c[i]);
}
ret=min(ret,res+r[k]);
}else{
if(s1==s0+1) res+=b1.q1(p1[s1])+b2.q1(p1[s1]);
res+=r[k];
for(re int i=t;i>=2;--i){
ret=min(ret,res);
res+=b1.q1(c[i])-b1.q0(c[i]);
res+=b1.q1(c[i-1])-b1.q0(c[i-1]);
if(c[i-1]<c[i]) ++res;else --res;
}
ret=min(ret,res);
}
for(re int i=1;i<=t;++i) b2.add(c[i],-1);
}
int a[N],b[N];
vector<int> p[N][2];
inline void rmain(){
int n=win(),m=0;b1.n=b2.n=n;
for(re int i=1,k=1;i<=n;++i,k=-k) a[i]=k*win(),b[i]=abs(a[i]);
for(re int i=1;i<=n;++i) debug("%d%c",a[i]," \n"[i==n]);
sort(b+1,b+n+1),m=unique(b+1,b+n+1)-b-1;
for(re int i=1;i<=n;++i) p[lower_bound(b+1,b+m+1,abs(a[i]))-b][a[i]>0].push_back(i);
ll r[2]={0,0},v[2]={0,0};
// 0: -1,1,-1,1... 1: 1,-1,1,-1...
for(re int i=1,l=0;i<=m;++i){
if(!(b[i]==0||(i==1&&p[i][0].size()+p[i][1].size()<=1))){
r[0]=v[0],r[1]=v[1],v[0]=v[1]=inf;
tran(0,v[0],p[i][0],p[i][1],r,l);
tran(1,v[1],p[i][1],p[i][0],r,l);
}
l^=(p[i][0].size()&1)^(p[i][1].size()&1);
for(re int k:p[i][0]) b1.add(k,1);
for(re int k:p[i][1]) b1.add(k,1);
// debug("%lld %lld\n",v[0],v[1]);
}
printf("%lld\n",v[1]==inf?-1:v[1]);
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// int T=win();while(T--)
rmain();
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: 33132kb
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: 1ms
memory: 33276kb
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: 4
Accepted
time: 3ms
memory: 31396kb
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: 0ms
memory: 31396kb
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%