/* https://oj.uz/submission/497983 (user202729) */
#include <stdio.h>
#define N 1000000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long max(long long a, long long b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *zz;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (zz[ii[j]] == zz[i_])
j++;
else if (zz[ii[j]] < zz[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int ft[N * 3];
void update(int i, int n, int x) {
while (i < n) {
ft[i] += x;
i |= i + 1;
}
}
int query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int main() {
static int xx[N], yy[N], pp[N], dd[N], ii[N];
int n, h, i;
long long cnt, ans;
scanf("%d%*d", &n);
for (i = 0; i < n; i++)
scanf("%d%d", &xx[i], &yy[i]);
for (i = 0; i < n; i++)
ii[i] = i;
zz = xx, sort(ii, 0, n);
for (i = 0; i < n; i++)
xx[ii[i]] = i;
zz = yy, sort(ii, 0, n);
for (i = 0; i < n; i++)
yy[ii[i]] = i;
for (i = 0; i < n; i++)
pp[xx[i]] = yy[i];
for (i = 0; i < n; i++)
if (pp[i] < i)
pp[i] += n;
cnt = 0;
for (i = 0; i < n; i++) {
cnt += query(pp[i]) - query(pp[i] - n);
update(pp[i], n * 3, 1);
}
for (i = 0; i < n; i++)
dd[i] = pp[i] - i;
zz = dd, sort(ii, 0, n);
ans = cnt;
for (h = 0; h < n; h++) {
i = ii[h];
update(pp[i], n * 3, -1);
cnt += n - 1;
cnt -= query(pp[i]) * 2;
cnt -= (n - 1 - i - (query(n * 3 - 1) - query(n + pp[i]))) * 2;
ans = max(ans, cnt);
update(n + pp[i], n * 3, 1);
}
ans = (long long) n * (n - 1) / 2 - ans;
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%*d", &n);
| ^~~~~~~~~~~~~~~~~~
Main.c:62:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d", &xx[i], &yy[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
536 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
536 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
62 ms |
5356 KB |
Output is correct |
10 |
Correct |
63 ms |
5328 KB |
Output is correct |
11 |
Correct |
63 ms |
5332 KB |
Output is correct |
12 |
Correct |
62 ms |
5304 KB |
Output is correct |
13 |
Correct |
63 ms |
5324 KB |
Output is correct |
14 |
Correct |
47 ms |
5020 KB |
Output is correct |
15 |
Correct |
52 ms |
4936 KB |
Output is correct |
16 |
Correct |
54 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
540 KB |
Output is correct |
5 |
Correct |
3 ms |
568 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
536 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
62 ms |
5356 KB |
Output is correct |
10 |
Correct |
63 ms |
5328 KB |
Output is correct |
11 |
Correct |
63 ms |
5332 KB |
Output is correct |
12 |
Correct |
62 ms |
5304 KB |
Output is correct |
13 |
Correct |
63 ms |
5324 KB |
Output is correct |
14 |
Correct |
47 ms |
5020 KB |
Output is correct |
15 |
Correct |
52 ms |
4936 KB |
Output is correct |
16 |
Correct |
54 ms |
4952 KB |
Output is correct |
17 |
Correct |
874 ms |
50928 KB |
Output is correct |
18 |
Correct |
871 ms |
50888 KB |
Output is correct |
19 |
Correct |
850 ms |
50888 KB |
Output is correct |
20 |
Correct |
875 ms |
50928 KB |
Output is correct |
21 |
Correct |
881 ms |
51016 KB |
Output is correct |
22 |
Correct |
869 ms |
50892 KB |
Output is correct |
23 |
Correct |
878 ms |
50888 KB |
Output is correct |
24 |
Correct |
632 ms |
47132 KB |
Output is correct |
25 |
Correct |
668 ms |
47004 KB |
Output is correct |
26 |
Correct |
683 ms |
47008 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |