#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAX_N = 300000;
int n, bit[MAX_N + 1];
pair<int, int> p[MAX_N];
void update(int h) {
for (; h <= n; h += h&-h) bit[h]++;
}
int query(int h) {
int s = 0;
for (; h; h -= h&-h) s += bit[h];
return s;
}
int main() {
scanf("%d", &n);
for (int i = 0; i <n; i++) {
scanf("%d", &p[i].first);
p[i].second = i + 1;
}
sort(p, p + n);
long long res = 0;
for (int i = n - 1, j = n - 1; i >= 0; i--) {
res += min(query(p[i].second - 1), query(n) - query(p[i].second));
if (!i || p[i].first == p[i - 1].first) continue;
for (; j >= i; j--) update(p[j].second);
}
printf("%lld", res);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4600 KB |
Output is correct |
2 |
Correct |
0 ms |
4600 KB |
Output is correct |
3 |
Correct |
1 ms |
4600 KB |
Output is correct |
4 |
Correct |
0 ms |
4600 KB |
Output is correct |
5 |
Correct |
0 ms |
4600 KB |
Output is correct |
6 |
Correct |
0 ms |
4600 KB |
Output is correct |
7 |
Correct |
0 ms |
4600 KB |
Output is correct |
8 |
Correct |
0 ms |
4600 KB |
Output is correct |
9 |
Correct |
0 ms |
4600 KB |
Output is correct |
10 |
Correct |
0 ms |
4600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4600 KB |
Output is correct |
2 |
Correct |
0 ms |
4600 KB |
Output is correct |
3 |
Correct |
0 ms |
4600 KB |
Output is correct |
4 |
Correct |
0 ms |
4600 KB |
Output is correct |
5 |
Correct |
0 ms |
4600 KB |
Output is correct |
6 |
Correct |
0 ms |
4600 KB |
Output is correct |
7 |
Correct |
0 ms |
4600 KB |
Output is correct |
8 |
Correct |
0 ms |
4600 KB |
Output is correct |
9 |
Correct |
0 ms |
4600 KB |
Output is correct |
10 |
Correct |
0 ms |
4600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4600 KB |
Output is correct |
2 |
Correct |
0 ms |
4600 KB |
Output is correct |
3 |
Correct |
0 ms |
4600 KB |
Output is correct |
4 |
Correct |
0 ms |
4600 KB |
Output is correct |
5 |
Correct |
0 ms |
4600 KB |
Output is correct |
6 |
Correct |
0 ms |
4600 KB |
Output is correct |
7 |
Correct |
0 ms |
4600 KB |
Output is correct |
8 |
Correct |
0 ms |
4600 KB |
Output is correct |
9 |
Correct |
0 ms |
4600 KB |
Output is correct |
10 |
Correct |
0 ms |
4600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
4600 KB |
Output is correct |
2 |
Correct |
41 ms |
4600 KB |
Output is correct |
3 |
Correct |
53 ms |
4600 KB |
Output is correct |
4 |
Correct |
82 ms |
4600 KB |
Output is correct |
5 |
Correct |
54 ms |
4600 KB |
Output is correct |
6 |
Correct |
26 ms |
4600 KB |
Output is correct |
7 |
Correct |
83 ms |
4600 KB |
Output is correct |
8 |
Correct |
105 ms |
4600 KB |
Output is correct |
9 |
Correct |
110 ms |
4600 KB |
Output is correct |
10 |
Correct |
111 ms |
4600 KB |
Output is correct |