#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
#include <time.h>
#include <functional>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
const int N_ = 300500;
int N, D[N_];
map<int, int> ren;
ll tb1[N_], tb2[N_];
const int SZ = 300500;
int tree[SZ+10];
void upd (int x, int d) {
for(; x > 0 && x <= SZ; x += x & -x) tree[x] += d;
}
int get (int x) {
int ret = 0;
for(; x > 0; x -= x & -x) ret += tree[x];
return ret;
}
int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i++) {
scanf("%d", &D[i]);
}
{
int v = 0;
for(int i = 1; i <= N; i++) ren[D[i]] = -1;
for(auto &it : ren) it.second = ++v;
for(int i = 1; i <= N; i++) D[i] = ren[D[i]];
}
{
memset(tree, 0, sizeof tree);
for(int i = 1; i <= N; i++) {
tb1[i] = (i-1) - get(D[i]);
upd(D[i], +1);
}
memset(tree, 0, sizeof tree);
for(int i = N; i > 0; i--) {
tb2[i] = (N-i) - get(D[i]);
upd(D[i], +1);
}
ll ans = 0;
for(int i = 0; i <= N; i++) {
ans += min(tb1[i], tb2[i]);
}
printf("%lld\n", ans);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8256 KB |
Output is correct |
2 |
Correct |
0 ms |
8256 KB |
Output is correct |
3 |
Correct |
0 ms |
8256 KB |
Output is correct |
4 |
Correct |
0 ms |
8256 KB |
Output is correct |
5 |
Correct |
0 ms |
8256 KB |
Output is correct |
6 |
Correct |
0 ms |
8256 KB |
Output is correct |
7 |
Correct |
0 ms |
8256 KB |
Output is correct |
8 |
Correct |
0 ms |
8256 KB |
Output is correct |
9 |
Correct |
0 ms |
8256 KB |
Output is correct |
10 |
Correct |
0 ms |
8256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8256 KB |
Output is correct |
2 |
Correct |
1 ms |
8256 KB |
Output is correct |
3 |
Correct |
0 ms |
8256 KB |
Output is correct |
4 |
Correct |
0 ms |
8256 KB |
Output is correct |
5 |
Correct |
0 ms |
8256 KB |
Output is correct |
6 |
Correct |
0 ms |
8256 KB |
Output is correct |
7 |
Correct |
0 ms |
8256 KB |
Output is correct |
8 |
Correct |
0 ms |
8256 KB |
Output is correct |
9 |
Correct |
0 ms |
8256 KB |
Output is correct |
10 |
Correct |
0 ms |
8256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8256 KB |
Output is correct |
2 |
Correct |
2 ms |
8256 KB |
Output is correct |
3 |
Correct |
0 ms |
8256 KB |
Output is correct |
4 |
Correct |
0 ms |
8388 KB |
Output is correct |
5 |
Correct |
4 ms |
8388 KB |
Output is correct |
6 |
Correct |
3 ms |
8256 KB |
Output is correct |
7 |
Correct |
0 ms |
8256 KB |
Output is correct |
8 |
Correct |
0 ms |
8388 KB |
Output is correct |
9 |
Correct |
0 ms |
8388 KB |
Output is correct |
10 |
Correct |
4 ms |
8388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
8256 KB |
Output is correct |
2 |
Correct |
111 ms |
12876 KB |
Output is correct |
3 |
Correct |
160 ms |
14196 KB |
Output is correct |
4 |
Correct |
235 ms |
16308 KB |
Output is correct |
5 |
Correct |
141 ms |
17496 KB |
Output is correct |
6 |
Correct |
45 ms |
12876 KB |
Output is correct |
7 |
Correct |
78 ms |
8256 KB |
Output is correct |
8 |
Correct |
211 ms |
22248 KB |
Output is correct |
9 |
Correct |
227 ms |
15252 KB |
Output is correct |
10 |
Correct |
273 ms |
22248 KB |
Output is correct |