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://codeforces.com/blog/entry/56372?#comment-400794 (koosaga) */
#include <stdio.h>
#define N 100000
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int max(int a, int 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;
}
}
void update(long long *ft, int i, int n, int x) {
while (i < n) {
ft[i] += x;
i |= i + 1;
}
}
long long query(long long *ft, int i) {
long long x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int aa_[N], bb_[N]; long long ft1[N], ft2[N], ft3[N], ft4[N];
int search(int *aa, int n, int a) {
int lower = -1, upper = n;
while (upper - lower > 1) {
int i_ = (lower + upper) / 2;
if (aa[i_] <= a)
lower = i_;
else
upper = i_;
}
return lower;
}
int main() {
static int xx[N], aa[N], bb[N], ii[N];
int n, i;
long long ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &xx[i]);
aa[i] = xx[i] - i, bb[i] = xx[i] + i;
}
for (i = 0; i < n; i++)
ii[i] = i;
zz = aa, sort(ii, 0, n);
for (i = 0; i < n; i++)
aa_[i] = aa[ii[i]], aa[ii[i]] = i;
zz = bb, sort(ii, 0, n);
for (i = 0; i < n; i++)
bb_[i] = bb[ii[i]], bb[ii[i]] = i;
ans = LINF;
for (i = 0; i < n; i++)
update(ft3, bb[i], n, 1), update(ft4, bb[i], n, bb_[bb[i]]);
for (i = 0; i < n; i++) {
int lower, upper, x, il, ir;
update(ft3, bb[i], n, -1), update(ft4, bb[i], n, -bb_[bb[i]]);
update(ft1, aa[i], n, 1), update(ft2, aa[i], n, aa_[aa[i]]);
lower = max(i + 1, n - i) - 1, upper = INF;
while (upper - lower > 1) {
x = (lower + upper) / 2;
if ((query(ft1, search(aa_, n, x - i)) + query(ft3, search(bb_, n, x + i))) * 2 >= n)
upper = x;
else
lower = x;
}
x = upper;
il = search(aa_, n, x - i), ir = search(bb_, n, x + i);
ans = min(ans, query(ft1, il) * (x - i) - query(ft2, il)
+ (query(ft2, n - 1) - query(ft2, il)) - (query(ft1, n - 1) - query(ft1, il)) * (x - i)
+ query(ft3, ir) * (x + i) - query(ft4, ir)
+ (query(ft4, n - 1) - query(ft4, ir)) - (query(ft3, n - 1) - query(ft3, ir)) * (x + i));
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
krov.c: In function 'main':
krov.c:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
krov.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &xx[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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |