This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |