/* upsolve after reading analysis */
#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define L 5 /* L = ceil(log2(N - 1)) */
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long dp[1 << 10][1 << 10]; int mask[N];
void init() {
int b1, b2, d, i;
for (b2 = 0; b2 < 1 << 10; b2++)
for (b1 = 0; b1 < 1 << 10; b1++) {
for (d = 1; d < 9; d++)
if ((b1 & 1 << d) == b1 && (b2 & 1 << d + 1) == b2) {
dp[b1][b2] = d;
break;
}
if (dp[b1][b2] != 0)
continue;
if ((b1 & 1 << 9) == b1 && (b2 & (1 << 1 | 1 << 0)) == b2) {
dp[b1][b2] = 9;
continue;
}
dp[b1][b2] = INF;
for (d = 0; d < 10; d++)
dp[b1][b2] = min(dp[b1][b2], d < 9 ? dp[b1 & ~(1 << d) | b2 & ~(1 << d + 1)][0] * 10 + d : dp[b1 & ~(1 << 9)][b2 & ~(1 << 0)] * 10 + d);
}
for (i = 1; i < N; i++)
mask[i] = mask[i / 10] | 1 << i % 10;
}
int bb[L + 1][N];
long long solve(int h, int n, int zero) {
int i, d;
long long ans;
if (!zero) {
int good = 1;
for (i = 0; i < n; i++)
if ((mask[i] & bb[h][i]) != bb[h][i]) {
good = 0;
break;
}
if (good)
return 0;
}
if (n <= 2)
return dp[bb[h][0]][n == 1 ? 0 : bb[h][1]];
ans = INF;
for (d = 0; d < 10; d++) {
int n_, b;
n_ = 0, b = 0;
for (i = 0; i < n; i++) {
b |= bb[h][i] & ~(1 << (d + i) % 10);
if (i == n - 1 || (d + i) % 10 == 9) {
bb[h + 1][n_++] = b;
b = 0;
}
}
ans = min(ans, solve(h + 1, n_, d == 0) * 10 + d);
}
return ans;
}
int main() {
int n, i;
init();
scanf("%d", &n);
for (i = 0; i < n; i++) {
int d;
scanf("%d", &d);
bb[0][i] = 1 << d;
}
printf("%lld\n", solve(0, n, 0));
return 0;
}
Compilation message
sequence.c: In function 'init':
sequence.c:19:45: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
19 | if ((b1 & 1 << d) == b1 && (b2 & 1 << d + 1) == b2) {
| ~~^~~
sequence.c:31:76: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
31 | dp[b1][b2] = min(dp[b1][b2], d < 9 ? dp[b1 & ~(1 << d) | b2 & ~(1 << d + 1)][0] * 10 + d : dp[b1 & ~(1 << 9)][b2 & ~(1 << 0)] * 10 + d);
| ~~^~~
sequence.c:31:48: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
31 | dp[b1][b2] = min(dp[b1][b2], d < 9 ? dp[b1 & ~(1 << d) | b2 & ~(1 << d + 1)][0] * 10 + d : dp[b1 & ~(1 << 9)][b2 & ~(1 << 0)] * 10 + d);
| ~~~^~~~~~~~~~~
sequence.c: In function 'main':
sequence.c:77:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
sequence.c:81:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d", &d);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8872 KB |
Output is correct |
2 |
Correct |
54 ms |
8860 KB |
Output is correct |
3 |
Correct |
53 ms |
8876 KB |
Output is correct |
4 |
Correct |
38 ms |
8856 KB |
Output is correct |
5 |
Correct |
40 ms |
8872 KB |
Output is correct |
6 |
Correct |
38 ms |
8876 KB |
Output is correct |
7 |
Correct |
41 ms |
8868 KB |
Output is correct |
8 |
Correct |
37 ms |
8876 KB |
Output is correct |
9 |
Correct |
45 ms |
8872 KB |
Output is correct |
10 |
Correct |
42 ms |
8784 KB |
Output is correct |
11 |
Correct |
42 ms |
8868 KB |
Output is correct |
12 |
Correct |
42 ms |
8888 KB |
Output is correct |
13 |
Correct |
37 ms |
8872 KB |
Output is correct |
14 |
Correct |
40 ms |
8880 KB |
Output is correct |
15 |
Correct |
40 ms |
8856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
8860 KB |
Output is correct |
2 |
Correct |
41 ms |
8868 KB |
Output is correct |
3 |
Correct |
44 ms |
8856 KB |
Output is correct |
4 |
Correct |
39 ms |
8872 KB |
Output is correct |
5 |
Correct |
40 ms |
8868 KB |
Output is correct |
6 |
Correct |
48 ms |
8860 KB |
Output is correct |
7 |
Correct |
43 ms |
8876 KB |
Output is correct |
8 |
Correct |
48 ms |
8860 KB |
Output is correct |
9 |
Correct |
57 ms |
8880 KB |
Output is correct |
10 |
Correct |
39 ms |
8856 KB |
Output is correct |
11 |
Correct |
41 ms |
8780 KB |
Output is correct |
12 |
Correct |
41 ms |
8860 KB |
Output is correct |
13 |
Correct |
59 ms |
8872 KB |
Output is correct |
14 |
Correct |
38 ms |
8780 KB |
Output is correct |
15 |
Correct |
42 ms |
8856 KB |
Output is correct |
16 |
Correct |
39 ms |
8864 KB |
Output is correct |
17 |
Correct |
39 ms |
8872 KB |
Output is correct |
18 |
Correct |
46 ms |
8872 KB |
Output is correct |
19 |
Correct |
43 ms |
8872 KB |
Output is correct |
20 |
Correct |
40 ms |
8860 KB |
Output is correct |
21 |
Correct |
40 ms |
8872 KB |
Output is correct |
22 |
Correct |
40 ms |
8860 KB |
Output is correct |
23 |
Correct |
37 ms |
8860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
8876 KB |
Output is correct |
2 |
Correct |
43 ms |
8908 KB |
Output is correct |
3 |
Correct |
52 ms |
8908 KB |
Output is correct |
4 |
Correct |
43 ms |
8860 KB |
Output is correct |
5 |
Correct |
38 ms |
8860 KB |
Output is correct |
6 |
Correct |
40 ms |
8868 KB |
Output is correct |
7 |
Correct |
52 ms |
9252 KB |
Output is correct |
8 |
Correct |
45 ms |
9164 KB |
Output is correct |
9 |
Correct |
76 ms |
9512 KB |
Output is correct |
10 |
Correct |
59 ms |
9500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
8872 KB |
Output is correct |
2 |
Correct |
36 ms |
8780 KB |
Output is correct |
3 |
Correct |
38 ms |
8856 KB |
Output is correct |
4 |
Correct |
49 ms |
8872 KB |
Output is correct |
5 |
Correct |
49 ms |
9164 KB |
Output is correct |
6 |
Correct |
44 ms |
8868 KB |
Output is correct |
7 |
Correct |
38 ms |
8860 KB |
Output is correct |
8 |
Correct |
36 ms |
8780 KB |
Output is correct |
9 |
Correct |
40 ms |
8856 KB |
Output is correct |
10 |
Correct |
41 ms |
8860 KB |
Output is correct |
11 |
Correct |
69 ms |
9512 KB |
Output is correct |
12 |
Correct |
54 ms |
9484 KB |
Output is correct |
13 |
Correct |
36 ms |
8780 KB |
Output is correct |
14 |
Correct |
38 ms |
8872 KB |
Output is correct |
15 |
Correct |
39 ms |
8872 KB |
Output is correct |
16 |
Correct |
43 ms |
8868 KB |
Output is correct |
17 |
Correct |
49 ms |
8876 KB |
Output is correct |
18 |
Correct |
42 ms |
8876 KB |
Output is correct |
19 |
Correct |
38 ms |
8856 KB |
Output is correct |
20 |
Correct |
38 ms |
8812 KB |
Output is correct |
21 |
Correct |
38 ms |
8876 KB |
Output is correct |
22 |
Correct |
35 ms |
8876 KB |
Output is correct |
23 |
Correct |
39 ms |
8780 KB |
Output is correct |
24 |
Correct |
48 ms |
8868 KB |
Output is correct |
25 |
Correct |
40 ms |
8872 KB |
Output is correct |
26 |
Correct |
37 ms |
8868 KB |
Output is correct |
27 |
Correct |
41 ms |
8908 KB |
Output is correct |
28 |
Correct |
37 ms |
8872 KB |
Output is correct |
29 |
Correct |
43 ms |
8864 KB |
Output is correct |
30 |
Correct |
37 ms |
8908 KB |
Output is correct |
31 |
Correct |
50 ms |
8860 KB |
Output is correct |
32 |
Correct |
50 ms |
9256 KB |
Output is correct |
33 |
Correct |
46 ms |
9116 KB |
Output is correct |
34 |
Correct |
56 ms |
9508 KB |
Output is correct |
35 |
Correct |
57 ms |
9392 KB |
Output is correct |
36 |
Correct |
54 ms |
9292 KB |
Output is correct |
37 |
Correct |
54 ms |
9516 KB |
Output is correct |
38 |
Correct |
46 ms |
9124 KB |
Output is correct |
39 |
Correct |
60 ms |
9508 KB |
Output is correct |
40 |
Correct |
53 ms |
9508 KB |
Output is correct |