#include <stdio.h>
#include <string.h>
#define N 1000000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(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];
void update(int i, int n) {
while (i < n) {
ft[i]++;
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], ii[N], pp[N];
int n, l, h, i;
long long k, ans;
scanf("%d%d", &n, &l);
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];
ans = INF;
for (h = 0; h < n; h++) {
memset(ft, 0, n * sizeof *ft);
k = 0;
for (i = 0; i < n; i++) {
k += query(n - 1 - pp[i]);
update(n - 1 - pp[i], n);
}
for (i = 0; i < n; i++)
ans = min(ans, k += n - 1 - pp[i] * 2);
for (i = 0; i < n; i++)
pp[i] = (pp[i] + 1) % n;
}
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, &l);
| ^~~~~~~~~~~~~~~~~~~~~
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]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
660 ms |
404 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
660 ms |
404 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
660 ms |
404 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |