This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 100000
long long abs_(long long a) { return a > 0 ? a : -a; }
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 tt[N];
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 (tt[ii[j]] == tt[i_])
j++;
else if (tt[ii[j]] < tt[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 main() {
static int xx[N], ii[N];
int n, i, t;
long long a, ans1, ans2;
scanf("%d", &n);
if (n == 1) {
printf("0\n");
return 0;
}
for (i = 0; i < n; i++)
scanf("%d", &xx[i]);
for (i = 1; i < n; i++)
xx[i] -= xx[0];
for (i = 1; i < n; i++)
tt[i] = xx[i] / i;
for (i = 1; i < n; i++)
ii[i] = i;
sort(ii, 1, n);
a = (long long) n * (n - 1) / 2, t = 0;
for (i = 1; i < n; i++)
if ((a -= ii[i] * 2) <= 0) {
t = tt[ii[i]];
break;
}
ans1 = 0;
for (i = 1; i < n; i++)
ans1 += abs_(xx[i] - (long long) t * i);
ans2 = 0;
for (i = 1; i < n; i++)
ans2 += abs_(xx[i] - (long long) (t + 1) * i);
printf("%lld\n", min(ans1, ans2));
return 0;
}
Compilation message (stderr)
pole.c: In function 'main':
pole.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
pole.c:46:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | 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... |