#pragma warning(disable:4996)
#include<stdio.h>
#include<algorithm>
int n, w[300010];
using namespace std;
struct A{
int a, num;
bool operator <(const A &p)const{
return a != p.a ? a < p.a : num < p.num;
}
}ord[300010];
int BIT[300010];
int Sum(int x){
int r = 0;
while (x){
r += BIT[x];
x -= (x&-x);
}
return r;
}
void Add(int x){
while (x <= n){
BIT[x]++;
x += (x&-x);
}
}
long long Res;
int main()
{
int i, S, j, pv1, pv2, t, S2, cnt = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++){
scanf("%d", &w[i]);
ord[i].a = w[i], ord[i].num = i;
}
sort(ord + 1, ord + n + 1);
for (i = 1; i <= n; i++){
for (j = i+1; j <= n; j++){
if (ord[i].a != ord[j].a)break;
}
t = j;
pv1 = i, pv2 = t - 1;
while (pv1 <= pv2){
cnt++;
S = ord[pv1].num - 1 - Sum(ord[pv1].num);
S2 = n - cnt - (ord[pv2].num - 1 - Sum(ord[pv2].num));
if (S < S2){
Add(ord[pv1].num);
Res += S;
pv1++;
}
else{
Add(ord[pv2].num);
Res += S2;
pv2--;
}
}
i = t - 1;
}
printf("%lld\n", Res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5776 KB |
Output is correct |
2 |
Correct |
0 ms |
5776 KB |
Output is correct |
3 |
Correct |
0 ms |
5776 KB |
Output is correct |
4 |
Correct |
0 ms |
5776 KB |
Output is correct |
5 |
Correct |
0 ms |
5776 KB |
Output is correct |
6 |
Correct |
0 ms |
5776 KB |
Output is correct |
7 |
Correct |
0 ms |
5776 KB |
Output is correct |
8 |
Correct |
0 ms |
5776 KB |
Output is correct |
9 |
Correct |
0 ms |
5776 KB |
Output is correct |
10 |
Correct |
0 ms |
5776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5776 KB |
Output is correct |
2 |
Correct |
36 ms |
5776 KB |
Output is correct |
3 |
Correct |
52 ms |
5776 KB |
Output is correct |
4 |
Correct |
64 ms |
5776 KB |
Output is correct |
5 |
Correct |
60 ms |
5776 KB |
Output is correct |
6 |
Correct |
32 ms |
5776 KB |
Output is correct |
7 |
Correct |
100 ms |
5776 KB |
Output is correct |
8 |
Correct |
108 ms |
5776 KB |
Output is correct |
9 |
Correct |
120 ms |
5776 KB |
Output is correct |
10 |
Correct |
100 ms |
5776 KB |
Output is correct |