QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#118196 | #6627. Line Town | xtqqwq# | 0 | 9ms | 37096kb | C++14 | 4.1kb | 2023-07-03 10:36:57 | 2024-05-31 18:50:13 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
int a[500005],p[500005],val[500005];
ll sum[500005],sum2[500005];
vector<int> vec[500005][2];
void add(int x,int c){
for(;x<=n;x+=(x&(-x))) val[x]+=c;
}
int ask(int x){
int ret=0;
for(;x;x-=(x&(-x))) ret+=val[x];
return ret;
}
int main(){
n=readint();
for(int i=1;i<=n;i++) a[i]=readint();
for(int i=1;i<=n;i++) if(i&1) a[i]*=-1;
// for(int i=1;i<=n;i++) cout<<a[i]<<' ';
// cout<<endl;
for(int i=1;i<=n;i++) if(a[i]!=0) p[++m]=abs(a[i]);
sort(p+1,p+m+1);
m=unique(p+1,p+m+1)-p-1;
for(int i=1;i<=n;i++) if(a[i]!=0) a[i]=(lower_bound(p+1,p+m+1,abs(a[i]))-p)*(a[i]<0?-1:1);
for(int i=1;i<=n;i++) vec[abs(a[i])][a[i]>0].pb(i);
for(int i=1;i<=n;i++) add(i,1);
int num=n,st=1,all=n;
ll ans=0;
for(int i=m;i>=1;i--){
int ed=st^(num&1)^1;
// cout<<"########## "<<i<<' '<<st<<' '<<ed<<endl;
int tmp=vec[i][1].size()-vec[i][0].size();
int t1=st==1?1:-1;
int t2=ed==0?1:-1;
if(tmp>max(t1,0)+max(t2,0)) return printf("-1\n"),0;
if(tmp<min(t1,0)+min(t2,0)) return printf("-1\n"),0;
for(auto r:vec[i][0]) add(r,-1),all--;
for(auto r:vec[i][1]) add(r,-1),all--;
ll mina=1ll<<60; int cho=0;
for(int j=-1;j<=1;j++){
int k=tmp-j;
if(abs(k)>1) continue;
if(abs(j-t1)>1) continue;
if(abs(k-t2)>1) continue;
int pl0=0,pl1=0,tmp=0,up0=vec[i][0].size(),up1=vec[i][1].size();
ll total=0;
for(int t=0;t<max(vec[i][0].size(),vec[i][1].size());t++) sum[t]=sum2[t]=0;
if(j==1) total+=ask(vec[i][1][0]),pl1++;
else if(j==-1) total+=ask(vec[i][0][0]),pl0++;
if(k==1) total+=all-ask(vec[i][1].back()),up1--;
else if(k==-1) total+=all-ask(vec[i][0].back()),up0--;
int tpl0=pl0,tpl1=pl1;
for(int t=pl0;t<up0;t++){
while(tpl0<up0&&vec[i][0][tpl0]<vec[i][1][t]) tpl0++;
while(tpl1<up1&&vec[i][1][tpl1]<vec[i][0][t]) tpl1++;
ll cost=ask(vec[i][0][t])+ask(vec[i][1][t]);
if((vec[i][0][t]<vec[i][1][t])!=(st^(j!=0))) cost++;
if(k!=0){
if(k==1&&vec[i][1].back()<vec[i][0][t]) cost++;
if(k==-1&&vec[i][0].back()<vec[i][1][t]) cost++;
}
sum[t]+=cost;
if(vec[i][0][t]<vec[i][1][t]){
// every t+1~tpl0-1>mid cost++
// mid>=t
// mid=t: tpl0-t-1;
// mid=t+1: tpl0-t-2
// ...
// mid=tpl0-1: 0
sum[t]+=tpl0-t-1;
sum2[t+1]--;
sum2[tpl0]++;
}
else{
// every t+1~tpl1-1 > mid cost++
sum[t]+=tpl1-t-1;
sum2[t+1]--;
sum2[tpl1]++;
}
cost=all-ask(vec[i][0][t])+all-ask(vec[i][1][t]);
if((vec[i][0][t]>vec[i][1][t])!=(ed^(k!=0))) cost++;
if(j!=0){
if(j==1&&vec[i][1][0]>vec[i][0][t]) cost++;
if(j==-1&&vec[i][0][0]>vec[i][1][t]) cost++;
}
// cout<<"cost "<<cost<<endl;
sum[t]-=cost;
total+=cost;
if(vec[i][0][t]<vec[i][1][t]){
// every tpl1~t-1 <= mid cost++
// mid < t
// mid=t-1: t-tpl1
// mid=t-2: t-tpl1-1
// mid=tpl1: 1
sum2[tpl1]++;
sum2[t]--;
}
else{
// every tpl0~t-1 <=mid cost++
sum2[tpl0]++;
sum2[t]--;
}
}
for(int t=pl0+1;t<up0;t++) sum2[t]+=sum2[t-1];
for(int t=pl0;t<up0;t++) sum[t]+=sum2[t];
for(int t=pl0+1;t<up0;t++) sum[t]+=sum[t-1];
if(chkmin(mina,total)) cho=j;
for(int t=pl0;t<up0;t++) if(chkmin(mina,sum[t]+total)) cho=j;
// cout<<"test "<<i<<' '<<j<<' '<<mina<<endl;
// cout<<total<<' ';
// for(int t=pl0;t<up0;t++) cout<<sum[t]+total<<' ';
// cout<<endl;
}
ans+=mina;
int tj=cho;
int tk=tmp-tj;
if(tj!=0) st^=1;
num-=vec[i][0].size()-vec[i][1].size();
}
printf("%lld\n",ans);
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 3
Accepted
time: 4ms
memory: 33856kb
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: 3ms
memory: 37096kb
input:
10 1 1 1 1 1 1 -1 1 1 -1
output:
2
result:
wrong answer 1st numbers differ - expected: '3', found: '2'
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: 37084kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
-1
result:
ok 1 number(s): "-1"
Test #61:
score: 0
Accepted
time: 4ms
memory: 34932kb
input:
10 -9 0 -1 7 5 10 6 3 2 -8
output:
13
result:
ok 1 number(s): "13"
Test #62:
score: 0
Accepted
time: 9ms
memory: 35756kb
input:
2000 40667 -598150 -1084780 1201651 1570514 -1859539 -2029075 2941581 -2945945 3038404 3447919 5293872 -5335692 -5669647 5973784 6041345 6346915 -7222112 8820986 -9153143 9563103 9749206 -9894732 -11847193 11987150 12161864 13336572 13528051 -13722732 -13836176 -15497141 -15841563 15862227 16618123 ...
output:
-1
result:
ok 1 number(s): "-1"
Test #63:
score: -4
Wrong Answer
time: 0ms
memory: 36648kb
input:
2000 3038404 -798315545 693574695 172661079 516504064 164016456 193562146 -131746730 382134316 -398886978 188767854 -834289064 -965673210 -826409444 -281381674 450991903 -592752625 81651101 -594873306 -352059270 -651772982 540062674 -769881300 68999588 307151563 -129950325 550154987 354801227 840540...
output:
738279
result:
wrong answer 1st numbers differ - expected: '658039', found: '738279'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%