# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
473645 |
2021-09-15T19:08:18 Z |
rainboy |
Vrtić (COCI18_vrtic) |
C |
|
1 ms |
460 KB |
#include <stdio.h>
#include <string.h>
#define N 150
#define M 20
int min(int a, int 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;
}
void sort(int *xx, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, x = xx[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx[j] == x)
j++;
else if (xx[j] < x) {
tmp = xx[i], xx[i] = xx[j], xx[j] = tmp;
i++, j++;
} else {
k--;
tmp = xx[j], xx[j] = xx[k], xx[k] = tmp;
}
sort(xx, l, i);
l = k;
}
}
int main() {
static char visited[N];
static int pp[N], xx[N], yy[N], ww[N][N], ii[M], cc[M], dp[1 << M];
int n, g, h, i, j, k, b, m;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &pp[i]), pp[i]--;
m = 0;
for (i = 0; i < n; i++)
if (!visited[i]) {
ii[m] = i;
while (!visited[i])
cc[m]++, visited[i] = 1, i = pp[i];
m++;
}
for (i = 0; i < n; i++)
scanf("%d", &xx[i]);
sort(xx, 0, n);
for (i = 0; i < n; i++)
for (j = i; j < n; j++)
ww[i][j] = i == j ? 0 : (i + 1 == j ? xx[j] - xx[i] : max(ww[i][j - 1], xx[j] - xx[j - 2]));
memset(dp, 0x3f, (1 << m) * sizeof *dp), dp[0] = 0;
for (b = 0; b < 1 << m; b++) {
k = 0;
for (h = 0; h < m; h++)
if ((b & 1 << h) != 0)
k += cc[h];
for (h = 0; h < m; h++)
if ((b & 1 << h) == 0)
dp[b | 1 << h] = min(dp[b | 1 << h], max(dp[b], ww[k][k + cc[h] - 1]));
}
b = (1 << m) - 1, k = n;
while (b) {
for (h = 0; h < m; h++)
if ((b & 1 << h) != 0 && dp[b] == max(dp[b ^ 1 << h], ww[k - cc[h]][k - 1]))
break;
i = ii[h];
for (g = 0; g < cc[h]; g += 2)
yy[i] = xx[k - 1 - g], i = pp[i];
for (g = cc[h] % 2 == 0 ? cc[h] - 1 : cc[h] - 2; g >= 0; g -= 2)
yy[i] = xx[k - 1 - g], i = pp[i];
b ^= 1 << h, k -= cc[h];
}
printf("%d\n", dp[(1 << m) - 1]);
for (i = 0; i < n; i++)
printf("%d ", yy[i]);
printf("\n");
return 0;
}
Compilation message
vrtic.c: In function 'main':
vrtic.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
vrtic.c:42:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
vrtic.c:52:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d", &xx[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |