QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125903 | #6627. Line Town | starrylasky | 0 | 4ms | 28216kb | C++14 | 4.3kb | 2023-07-17 21:32:11 | 2023-07-17 21:32:14 |
Judging History
answer
///yl/hs/bx/yl
#include<bits/stdc++.h>
#define fep(i,l,r) for(int i=l;i<=r;++i)
#define feb(i,r,l) for(int i=r;i>=l;--i)
#define For(i,u) for(int i=head[u];i;i=e[i].nxt)
#define LL long long
// #define int long long
#define ld long double
#define pr pair<int,int>
#define mpr make_pair
using namespace std;
const int N = 5e5+5,mod = 1e9+7;
const LL INF = 1e15;
inline int read()
{
int s=0,w=1; char ch=getchar();
while(!(ch>='0'&&ch<='9')) {if(ch=='-') w=-1; ch=getchar();}
while( ch>='0'&&ch<='9') {s=(s<<1)+(s<<3)+ch-'0'; ch=getchar();}
return s*w;
}
inline int Mod(int x) {return x>=mod?x-mod:x;}
inline void addmod(int &x,int y) {x=Mod(x+y);}
inline void chkmin(LL &x,LL y) {x=min(x,y);}
namespace starrylasky
{
int n,num,S,a[N],b[N],rk[N],id[N],col[N]; vector<int > v[N];
LL f[2],g[2];
struct BIT
{
#define lowbit(x) (x&-x)
int sum[N];
inline void update(int x,int res) {for(;x<=n;x+=lowbit(x)) sum[x]+=res;}
inline int query(int x)
{
int ans=0;
for(;x;x-=lowbit(x)) ans+=sum[x];
return ans;
}
}b1,b2;
inline LL calc(int m)
{
LL ans=0;// cerr<<m<<"\n";
feb(i,m,1) ans+=b2.query(rk[i]),b2.update(rk[i],1);
fep(i,1,m) b2.update(rk[i],-1);
return ans;
}
inline void work(int x,int cl,int cr)
{
if(g[cl]==INF) return ;
int siz=v[x].size(),cnt0=0,cnt1=0,c0=(siz+1)/2,c1=siz/2,n0=0,n1=0;
vector<int > a1,a0; a1.resize(siz); a0.resize(siz);
fep(i,1,siz)
{
rk[i]=b1.query(v[x][i-1]);
if(col[v[x][i-1]]) a1[++n1]=i,++cnt1;
else a0[++n0]=i,++cnt0;
}
if(cr) swap(c0,c1);
if(cl==(cr^((siz-1)&1)))
{
if(cnt0!=c0||cnt1!=c1) return ;
for(int i=1,j=1,k=1;i<=siz;i++) id[i]=(cr^((siz-i)&1)?a1[j++]:a0[k++]);
LL s=calc(siz);
fep(i,1,siz) s+=num-(siz-i)-rk[i];
chkmin(f[cl],g[cl]+s);// cerr<<x<<" "<<s<<" ";
fep(i,1,siz)
{
int j=id[i];
s+=rk[j]-j-(num-(siz-j)-rk[j]);// cerr<<s<<" ";
chkmin(f[cl^(i&1)],g[cl]+s);
}
// cerr<<"\n";
}
else if(c0==cnt0&&c1==cnt1)
{
for(int i=1,j=1,k=1;i<=siz;i++) id[i]=(cr^((siz-i)&1)?a1[j++]:a0[k++]);
LL s=calc(siz);
fep(i,1,siz) s+=num-(siz-i)-rk[i];
chkmin(f[cl],g[cl]+s);// cerr<<x<<" "<<s<<" ";
for(int i=1;i+1<=siz;i+=2)
{
int j=id[i],k=id[i+1];
s+=rk[j]-j-(num-(siz-j)-rk[j])+rk[k]-k-(num-(siz-k)-rk[k])+(rk[j]<rk[k]?1:-1);
chkmin(f[cl],g[cl]+s);// cerr<<s<<" ";
}
// cerr<<"\n";
}
else
{
(cr^((siz-1)&1)?c1:c0)--,(cl?c1:c0)--;
if(c1!=cnt1||c0!=cnt0) return ;
for(int i=1,j=1,k=1;i<=siz;i++)
if(i==1) id[i]=(cl?a1[j++]:a0[k++]);
else id[i]=(cr^(siz-i&1)?a1[j++]:a0[k++]);
LL s=calc(siz);
fep(i,1,siz) s+=num-(siz-i)-rk[i];
s+=rk[1]-1-(num-(siz-1)-rk[1]);
chkmin(f[cl^1],g[cl]+s);
for(int i=2;i+1<=siz;i+=2)
{
int j=id[i],k=id[i+1];
s+=rk[j]-j-(num-(siz-j)-rk[j])+rk[k]-k-(num-(siz-k)-rk[k])+(rk[j]<rk[k]?1:-1);
chkmin(f[cl^1],g[cl]+s);
}
}
}
inline void Main()
{
n=S=read(); num=n;
fep(i,1,n) a[i]=read(),b[i]=abs(a[i]);
sort(b+1,b+1+S); S=unique(b+1,b+1+S)-b-1;
fep(i,1,n)
{
int t=lower_bound(b+1,b+1+S,abs(a[i]))-b;
v[t].emplace_back(i); a[i]=(a[i]>0?t:-t);
b1.update(i,1); col[i]=(a[i]<0)^(i&1);
}
f[0]=0,f[1]=INF;
feb(i,S,1)
{
fep(j,0,1) g[j]=f[j],f[j]=INF;
fep(j,0,1) work(i,j,j^(num&1));
for(auto p:v[i]) b1.update(p,-1);
num-=v[i].size();
}
printf("%d\n",min(f[0],f[1])==INF?-1:min(f[0],f[1]));
}
}
signed main()
{
int _T=1;
while(_T--) starrylasky::Main();
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: 4ms
memory: 24100kb
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: 26116kb
input:
10 1 1 1 1 1 1 -1 1 1 -1
output:
0
result:
wrong answer 1st numbers differ - expected: '3', found: '0'
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: 28216kb
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: 28168kb
input:
10 -9 0 -1 7 5 10 6 3 2 -8
output:
23
result:
wrong answer 1st numbers differ - expected: '13', found: '23'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%