#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
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]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
444 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1368 KB |
Output is correct |
2 |
Correct |
10 ms |
2396 KB |
Output is correct |
3 |
Correct |
12 ms |
2476 KB |
Output is correct |
4 |
Correct |
10 ms |
2392 KB |
Output is correct |
5 |
Correct |
12 ms |
2396 KB |
Output is correct |
6 |
Correct |
11 ms |
2272 KB |
Output is correct |