QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#113782 | #6627. Line Town | 1kri# | 0 | 3ms | 32144kb | C++14 | 2.7kb | 2023-06-19 12:16:46 | 2024-05-31 14:07:36 |
Judging History
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 t0[1000005],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;
if (a[now].v==0)return 0;
int l=now,r=now;
while(l>1&&_abs(a[l-1].v)==_abs(a[r].v))l--;
int l0=0,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;
ll v0=inf,v1=inf;
int cnt0=0,cnt1=0;
for (int i=1;i<=len;i++)
if (get_o(o,i)==1)pos[i]=t0[++cnt0];
else pos[i]=t1[++cnt1];
if ((len&1)==(now&1)){
if (cnt0==l0&&cnt1==l1){
ll s=ask(len);
for (int i=1;i<=len;i++)s+=c[pos[i]][0];
for (int i=len;i>=0;i--){
if (get_o(o,i+1)==0)v0=min(v0,s);
else v1=min(v1,s);
if (i==0)break;
s-=c[pos[i]][0],s+=c[pos[i]][1];
}
}
}
else{
for (int i=0;i<=len;i++){
cnt0=0,cnt1=0;
ll s=0;
for (int j=1;j<=i;j++){
if (get_o(o,j)==1)pos[j]=t0[++cnt0];
else pos[j]=t1[++cnt1];
s+=c[pos[j]][0];
}
for (int j=i+1;j<=len;j++){
if (get_o(o,now-(len-j))==0)pos[j]=t0[++cnt0];
else pos[j]=t1[++cnt1];
s+=c[pos[j]][1];
}
if (cnt0==l0&&cnt1==l1){
if (get_o(o,i+1)==0)v0=min(v0,s+ask(len));
else v1=min(v1,s+ask(len));
}
}
}
ll ans=inf;
ans=min(ans,v0+dfs(l-1,0));
ans=min(ans,v1+dfs(l-1,1));
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[a[i].p][0]=bit.ask(a[i].p-1);
c[a[i].p][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;
}
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: 31000kb
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: 0ms
memory: 30360kb
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: 32144kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
18
result:
wrong answer 1st numbers differ - expected: '-1', found: '18'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%