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