QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125850 | #6627. Line Town | dengtingyu | 0 | 0ms | 0kb | C++14 | 3.2kb | 2023-07-17 19:41:06 | 2023-07-17 19:41:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 600020
#define inf 1e15
ll n;
ll a[N];
ll xu[N],cn=0;
struct shuz{
ll c[N];
#define lowbit(i) (i&(-i))
inline void update(ll x,ll y){
assert(x>=N);
for(int i=x;i<=n;i+=lowbit(i))c[i]+=y;
return ;
}inline ll ask(ll x){
assert(x>=N);
ll an=0;for(int i=x;i;i-=lowbit(i))an+=c[i];
return an;
}
};
vector<ll>ji[2][N],tem[2][2];
ll f[N][2];ll nw;ll siz;
inline void chuli(ll i,ll op){
for(int j=0;j<=1;j++)for(int k=0;k<=1;k++)tem[j][k].clear(),tem[j][k].resize(ji[j][i].size());
ll u=op^(nw&1);
for(int g=0;g<ji[op][i].size();g++){
ll sm1=upper_bound(ji[op^1][i].begin(),ji[op^1][i].end(),ji[op][i][g])-ji[op^1][i].begin();
tem[op][0][g]=ji[op][i][g]-sm1-g-1;tem[op][1][g]=nw-siz-tem[op][0][g];
tem[op][0][g]+=abs(g-sm1);
ll p=(ji[op][i].size()-g-1);if(u!=op)p++;
ll tt=ji[op^1][i].size()-p-sm1;if(tt<0)tt=-tt;
tem[op][1][g]+=tt;
}for(int g=0;g<ji[op^1][i].size();g++){
ll sm0=upper_bound(ji[op][i].begin(),ji[op][i].end(),ji[op^1][i][g])-ji[op][i].begin();
tem[op^1][0][g]=ji[op^1][i][g]-sm0-g-1,tem[op^1][1][g]=nw-siz-tem[op^1][0][g];
}for(int g=1;g<ji[op][i].size();g++)tem[op][0][g]+=tem[op][0][g-1];
for(int g=1;g<ji[op^1][i].size();g++)tem[op^1][0][g]+=tem[op^1][0][g-1];
for(int g=ji[op][i].size()-2;g>=0;g--)tem[op][1][g]+=tem[op][1][g+1];
for(int g=ji[op^1][i].size()-2;g>=0;g--)tem[op^1][1][g]+=tem[op^1][1][g+1];
reverse(tem[op^1][1].begin(),tem[op^1][1].end());
reverse(tem[op][1].begin(),tem[op][1].end());
return ;
}
ll kz[N];
int main()
{
// freopen("test1.in","r",stdin);
//freopen(".in","r",stdin);
//freopen("test1.out","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;for(int i=1;i<=n;i++)cin>>a[i],xu[++cn]=abs(a[i]);
for(int i=1;i<=n;i++)if(a[i]<0)a[i]=-a[i],kz[i]=1;
sort(xu+1,xu+cn+1);cn=unique(xu+1,xu+cn+1)-xu-1;assert(cn+1>=N);
for(int i=1;i<=n;i++)a[i]=lower_bound(xu+1,xu+cn+1,a[i])-xu;
shuz o;for(int i=1;i<=cn;i++)f[i][0]=f[i][1]=inf;
for(int i=1;i<=n;i++){
o.update(a[i],1);
ll g=o.ask(a[i]);
ji[kz[i]^(i&1)][a[i]].push_back(g);
}f[cn+1][1]=0;f[cn+1][0]=inf;nw=n;
for(int i=cn;i>=1;i--){
if((ji[0][i].empty()&&ji[1][i].empty())||xu[i]==0){
f[i][1]=f[i+1][1];f[i][0]=f[i+1][0];continue;
}ll o=f[i+1][0],pi=f[i+1][1];siz=ji[0][i].size()+ji[1][i].size();
chuli(i,1);//f[x][0]
ll p=(nw&1)^1;
for(int j=0;j<=siz;j++){
ll las=siz-j;
ll q1=(j+1)/2,h1=(las+p)/2,q0=j-q1,h0=las-h1;
if(q1+h1!=ji[1][i].size()||h0+q0!=ji[0][i].size())continue;
ll an=o+(q0==0?0:tem[0][0][q0-1])+(h0==0?0:tem[0][1][h0-1])+
(q1==0?0:tem[1][0][q1-1])+(h1==0?0:tem[1][1][h1-1]);
f[i][(j&1)]=min(f[i][(j&1)],an);
}
chuli(i,0);//f[x][1]
p=(nw&1);
for(int j=0;j<=siz;j++){
ll las=siz-j;
ll q1=j/2,h1=(las+p)/2,q0=j-q1,h0=las-h1;
if(q1+h1!=ji[1][i].size()||h0+q0!=ji[0][i].size())continue;
ll an=pi+(q0==0?0:tem[0][0][q0-1])+(h0==0?0:tem[0][1][h0-1])+
(q1==0?0:tem[1][0][q1-1])+(h1==0?0:tem[1][1][h1-1]);
f[i][(j&1)^1]=min(f[i][(j&1)^1],an);
}
nw-=siz;
}ll ans=min(f[1][0],f[1][1]);
if(ans>=inf)cout<<-1;
else cout<<ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
result:
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
Dangerous Syscalls
Test #60:
score: 0
Dangerous Syscalls
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%