#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using ll=long long;
const ll inf=1e18;
const int N=5e5+5;
int n,a[N],idx[N];
ll dp[N][2],ans;
struct FT{
int T[N]={0};
void upd(int i,int v=1){
for(;i<=n;i+=i&-i) T[i]+=v;
}
int qry(int i){
int r=0;
for(;i;i-=i&-i) r+=T[i];
return r;
}
}T;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
idx[i]=i;
}
sort(idx+1,idx+n+1,[&](int i,int j){
return abs(a[i])<abs(a[j]);
});
T.upd(idx[1]);
for(int i=2;i<=n;i++){
int p=(idx[i]%2+2222)%2;
dp[i][0]=dp[i][1]=inf;
int L=T.qry(idx[i]),R=i-1-L;
if(i&1){
if(a[idx[i]]<0){
dp[i][p]=dp[i-1][p]+L;
dp[i][p^1]=dp[i-1][p]+R;
} else{
dp[i][p]=dp[i-1][p^1]+R;
dp[i][p^1]=dp[i-1][p^1]+L;
}
} else{
if(a[idx[i]]<0){
dp[i][p^1]=min(dp[i-1][p]+R,dp[i-1][p^1]+L);
} else{
dp[i][p]=min(dp[i-1][p]+L,dp[i-1][p^1]+R);
}
}
//cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<idx[i]<<" "<<T.qry(idx[i])<<endl;
T.upd(idx[i]);
}
ans=dp[n][n%2];
if(ans>inf/2) ans=-1;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
76 ms |
14128 KB |
Output is correct |
15 |
Correct |
125 ms |
14076 KB |
Output is correct |
16 |
Correct |
117 ms |
14084 KB |
Output is correct |
17 |
Correct |
115 ms |
14124 KB |
Output is correct |
18 |
Correct |
117 ms |
14028 KB |
Output is correct |
19 |
Correct |
120 ms |
14028 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
115 ms |
14064 KB |
Output is correct |
23 |
Correct |
114 ms |
14076 KB |
Output is correct |
24 |
Correct |
62 ms |
14032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |