#include <stdio.h>
#include <string.h>
#define N 150
#define M 17
#define B 10000000
#define INF 0x3f3f3f3f
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[N + 1][N], kk[N + 1], kk_[N + 1], cc[M], bb[M], dp[B];
int n, n_, m, b_, g, h, i, j, b, c;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &pp[i]), pp[i]--;
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]));
c = 0;
for (i = 0; i < n; i++)
if (!visited[i]) {
c = 0;
while (!visited[i])
c++, visited[i] = 1, i = pp[i];
ii[c][kk[c]++] = i;
}
m = 0;
for (c = 1; c <= n; c++)
if (kk[c])
cc[m++] = c;
b_ = 1;
for (h = m - 1; h >= 0; h--)
bb[h] = b_, b_ *= kk[cc[h]] + 1;
dp[0] = 0;
for (b = 1; b < b_; b++) {
n_ = 0;
for (h = 0; h < m; h++)
n_ += (b / bb[h] % (kk[cc[h]] + 1)) * cc[h];
dp[b] = INF;
for (h = 0; h < m; h++)
if (b / bb[h] % (kk[cc[h]] + 1) > 0)
dp[b] = min(dp[b], max(dp[b - bb[h]], ww[n_ - cc[h]][n_ - 1]));
}
b = b_ - 1, n_ = n;
while (b > 0) {
for (h = 0; h < m; h++)
if (b / bb[h] % (kk[cc[h]] + 1) > 0 && dp[b] == max(dp[b - bb[h]], ww[n_ - cc[h]][n_ - 1]))
break;
i = ii[cc[h]][kk_[cc[h]]++];
for (g = 0; g < cc[h]; g += 2)
yy[i] = xx[n_ - 1 - g], i = pp[i];
for (g = cc[h] % 2 == 0 ? cc[h] - 1 : cc[h] - 2; g >= 0; g -= 2)
yy[i] = xx[n_ - 1 - g], i = pp[i];
b -= bb[h], n_ -= cc[h];
}
printf("%d\n", dp[b_ - 1]);
for (i = 0; i < n; i++)
printf("%d ", yy[i]);
printf("\n");
return 0;
}
Compilation message
vrtic.c: In function 'main':
vrtic.c:42:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
vrtic.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
vrtic.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 |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
59 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
612 KB |
Output is correct |
2 |
Correct |
446 ms |
5444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
460 KB |
Output is correct |
2 |
Correct |
454 ms |
5476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
712 KB |
Output is correct |
2 |
Correct |
1606 ms |
16848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
748 KB |
Output is correct |
2 |
Correct |
1582 ms |
16876 KB |
Output is correct |