#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, arr[303030];
vector<ll> com;
ll tree[1010101];
ll ans, cnt1[303030], cnt2[303030];
void init(ll v, ll st, ll ed) {
if (st == ed) {
tree[v] = 0;
return;
}
ll mid = (st + ed) / 2;
init(2 * v, st, mid);
init(2 * v + 1, mid + 1, ed);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
void update(ll v, ll st, ll ed, ll idx, ll val) {
if (st == idx && ed == idx) {
tree[v] += val;
return;
}
if (idx < st || idx > ed) return;
ll mid = (st + ed) / 2;
update(2 * v, st, mid, idx, val);
update(2 * v + 1, mid + 1, ed, idx, val);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
ll get(ll v, ll st, ll ed, ll lt, ll rt) {
if (st > rt || ed < lt) return 0;
if (lt <= st && ed <= rt) return tree[v];
ll mid = (st + ed) / 2;
return get(2 * v, st, mid, lt, rt) + get(2 * v + 1, mid + 1, ed, lt, rt);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (ll i = 1; i <= n; i++) cin >> arr[i];
for (ll i = 1; i <= n; i++) com.push_back(arr[i]);
sort(com.begin(), com.end());
com.erase(unique(com.begin(), com.end()), com.end());
for (ll i = 1; i <= n; i++) {
arr[i] = lower_bound(com.begin(), com.end(), arr[i]) - com.begin();
}
init(1, 0, com.size() - 1);
for (ll i = 1; i <= n; i++) {
cnt1[i] = get(1, 0, com.size() - 1, arr[i] + 1, com.size() - 1);
update(1, 0, com.size() - 1, arr[i], 1);
}
init(1, 0, com.size() - 1);
for (ll i = n; i >= 1; i--) {
cnt2[i] = get(1, 0, com.size() - 1, arr[i] + 1, com.size() - 1);
update(1, 0, com.size() - 1, arr[i], 1);
}
for (ll i = 1; i <= n; i++) {
ans += min(cnt1[i], cnt2[i]);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
248 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
672 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2288 KB |
Output is correct |
2 |
Correct |
95 ms |
5632 KB |
Output is correct |
3 |
Correct |
121 ms |
7168 KB |
Output is correct |
4 |
Correct |
169 ms |
10744 KB |
Output is correct |
5 |
Correct |
116 ms |
10816 KB |
Output is correct |
6 |
Correct |
66 ms |
5728 KB |
Output is correct |
7 |
Correct |
38 ms |
9856 KB |
Output is correct |
8 |
Incorrect |
203 ms |
17748 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |