# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
473653 |
2021-09-15T19:29:40 Z |
rainboy |
Vrtić (COCI18_vrtic) |
C |
|
41 ms |
772 KB |
#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], 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]));
}
printf("%d\n", dp[b_ - 1]);
return 0;
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];
}
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]);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Unexpected end of file - int32 expected |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
332 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
592 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
432 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
772 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
688 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |