#include <iostream>
#include <cstdio>
using namespace std;
#define N 100000
int mas[N][10][9];
long long best = 102345678900000LL;
int atb[17];
int K;
int B[N];
void check(int gar)
{
long long rez = 0;
for (int i = gar; i >= 0; i--)
{
rez = rez * 10 + atb[i];
}
if (rez < best)
{
best = rez;
}
}
void rek(int depth, int length, bool needFrontZero)
{
int digit,newLength,newNeedZero;
if (length == 1)
{
int dd = depth;
for (int i = 9; i >= 1; i--)
{
if (mas[0][i][depth])
{
atb[dd] = i;
dd++;
}
}
if (mas[0][0][depth])
{
if (dd > depth)
{
atb[dd] = atb[dd-1];
atb[dd-1] = 0;
dd++;
}
else
{
atb[dd] = 0;
atb[dd+1] = 1;
dd += 2;
}
}
if (dd == 0 || (depth == dd && needFrontZero))
{
atb[dd] = 1;
dd++;
}
check(dd-1);
}
else if (length == 2)
{
for (int j = 0; j < 9; j++)
{
atb[depth] = j;
digit = j;
newLength = 0;
newNeedZero = false;
if (j == 0 && mas[0][0][depth] == 1)
{
newNeedZero = true;
}
for (int k = 0; k < 10; k++)
{
mas[newLength][k][depth+1] = 0;
}
for (int i = 0; i < length; i++)
{
for (int k = 0; k < 10; k++)
{
if (mas[i][k][depth] && (k != digit))
{
mas[newLength][k][depth+1] = 1;
}
}
digit = (digit + 1) % 10;
}
rek(depth+1, 1, newNeedZero);
}
// 9 - > 0
atb[depth] = 9;
for (int j = 0; j < 9; j++)
{
atb[depth+1] = j;
digit = j;
newLength = 0;
newNeedZero = false;
if (j == 0 && mas[0][0][depth] == 1)
{
newNeedZero = true;
}
for (int k = 0; k < 10; k++)
{
mas[newLength][k][depth+2] = 0;
}
for (int i = 0; i < length; i++)
{
for (int k = 0; k < 10; k++)
{
if (mas[i][k][depth] && (k != digit) && (k != (9 + i) % 10))
{
mas[newLength][k][depth+2] = 1;
}
}
digit = (digit + 1) % 10;
}
rek(depth+2, 1, newNeedZero);
}
}
else
{
for (int j = 0; j < 10; j++)
{
atb[depth] = j;
digit = j;
newLength = 0;
newNeedZero = false;
if (j == 0 && mas[0][0][depth] == 1)
{
newNeedZero = true;
}
for (int k = 0; k < 10; k++)
{
mas[newLength][k][depth+1] = 0;
}
for (int i = 0; i < length; i++)
{
for (int k = 0; k < 10; k++)
{
if (mas[i][k][depth] && (k != digit))
{
mas[newLength][k][depth+1] = 1;
}
}
digit = (digit + 1) % 10;
if (digit == 0 && i < length -1)
{
newLength++;
for (int k = 0; k < 10; k++)
{
mas[newLength][k][depth+1] = 0;
}
}
}
rek(depth+1,newLength+1, newNeedZero);
}
}
}
long long recreate_sequence()
{
best = 102345678900000LL;
for (int i = 0; i < K; i++)
{
for (int j = 0; j < 10; j++)
{
mas[i][j][0] = 0;
}
mas[i][B[i]][0] = 1;
}
rek(0,K,true);
return best;
}
const int LENGTH = 6;
int ARRAY[LENGTH] = {7, 8, 9, 5, 1, 2};
int main()
{
cin >> K;
for (int i = 0; i < K; ++i)
cin >> B[i];
cout << recreate_sequence() << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37224 KB |
Output is correct |
2 |
Correct |
0 ms |
37224 KB |
Output is correct |
3 |
Correct |
0 ms |
37224 KB |
Output is correct |
4 |
Correct |
0 ms |
37224 KB |
Output is correct |
5 |
Correct |
0 ms |
37224 KB |
Output is correct |
6 |
Correct |
0 ms |
37224 KB |
Output is correct |
7 |
Correct |
0 ms |
37224 KB |
Output is correct |
8 |
Correct |
0 ms |
37224 KB |
Output is correct |
9 |
Correct |
0 ms |
37224 KB |
Output is correct |
10 |
Correct |
0 ms |
37224 KB |
Output is correct |
11 |
Correct |
0 ms |
37224 KB |
Output is correct |
12 |
Correct |
0 ms |
37224 KB |
Output is correct |
13 |
Correct |
0 ms |
37224 KB |
Output is correct |
14 |
Correct |
4 ms |
37224 KB |
Output is correct |
15 |
Correct |
4 ms |
37224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37224 KB |
Output is correct |
2 |
Correct |
0 ms |
37224 KB |
Output is correct |
3 |
Correct |
0 ms |
37224 KB |
Output is correct |
4 |
Correct |
0 ms |
37224 KB |
Output is correct |
5 |
Correct |
0 ms |
37224 KB |
Output is correct |
6 |
Correct |
0 ms |
37224 KB |
Output is correct |
7 |
Correct |
0 ms |
37224 KB |
Output is correct |
8 |
Correct |
0 ms |
37224 KB |
Output is correct |
9 |
Correct |
0 ms |
37224 KB |
Output is correct |
10 |
Correct |
0 ms |
37224 KB |
Output is correct |
11 |
Correct |
0 ms |
37224 KB |
Output is correct |
12 |
Correct |
0 ms |
37224 KB |
Output is correct |
13 |
Correct |
0 ms |
37224 KB |
Output is correct |
14 |
Correct |
0 ms |
37224 KB |
Output is correct |
15 |
Correct |
0 ms |
37224 KB |
Output is correct |
16 |
Correct |
4 ms |
37224 KB |
Output is correct |
17 |
Correct |
4 ms |
37224 KB |
Output is correct |
18 |
Correct |
0 ms |
37224 KB |
Output is correct |
19 |
Correct |
0 ms |
37224 KB |
Output is correct |
20 |
Correct |
0 ms |
37224 KB |
Output is correct |
21 |
Correct |
0 ms |
37224 KB |
Output is correct |
22 |
Correct |
4 ms |
37224 KB |
Output is correct |
23 |
Correct |
4 ms |
37224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37224 KB |
Output is correct |
2 |
Correct |
32 ms |
37224 KB |
Output is correct |
3 |
Correct |
32 ms |
37224 KB |
Output is correct |
4 |
Correct |
32 ms |
37224 KB |
Output is correct |
5 |
Correct |
36 ms |
37224 KB |
Output is correct |
6 |
Correct |
20 ms |
37224 KB |
Output is correct |
7 |
Correct |
232 ms |
37224 KB |
Output is correct |
8 |
Correct |
148 ms |
37224 KB |
Output is correct |
9 |
Correct |
328 ms |
37224 KB |
Output is correct |
10 |
Correct |
324 ms |
37224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
37224 KB |
Output is correct |
2 |
Correct |
0 ms |
37224 KB |
Output is correct |
3 |
Correct |
0 ms |
37224 KB |
Output is correct |
4 |
Correct |
0 ms |
37224 KB |
Output is correct |
5 |
Correct |
180 ms |
37224 KB |
Output is correct |
6 |
Correct |
0 ms |
37224 KB |
Output is correct |
7 |
Correct |
0 ms |
37224 KB |
Output is correct |
8 |
Correct |
0 ms |
37224 KB |
Output is correct |
9 |
Correct |
0 ms |
37224 KB |
Output is correct |
10 |
Correct |
0 ms |
37224 KB |
Output is correct |
11 |
Correct |
332 ms |
37224 KB |
Output is correct |
12 |
Correct |
336 ms |
37224 KB |
Output is correct |
13 |
Correct |
0 ms |
37224 KB |
Output is correct |
14 |
Correct |
0 ms |
37224 KB |
Output is correct |
15 |
Correct |
0 ms |
37224 KB |
Output is correct |
16 |
Correct |
0 ms |
37224 KB |
Output is correct |
17 |
Correct |
0 ms |
37224 KB |
Output is correct |
18 |
Correct |
0 ms |
37224 KB |
Output is correct |
19 |
Correct |
4 ms |
37224 KB |
Output is correct |
20 |
Correct |
4 ms |
37224 KB |
Output is correct |
21 |
Correct |
0 ms |
37224 KB |
Output is correct |
22 |
Correct |
0 ms |
37224 KB |
Output is correct |
23 |
Correct |
4 ms |
37224 KB |
Output is correct |
24 |
Correct |
0 ms |
37224 KB |
Output is correct |
25 |
Correct |
4 ms |
37224 KB |
Output is correct |
26 |
Correct |
4 ms |
37224 KB |
Output is correct |
27 |
Correct |
32 ms |
37224 KB |
Output is correct |
28 |
Correct |
28 ms |
37224 KB |
Output is correct |
29 |
Correct |
36 ms |
37224 KB |
Output is correct |
30 |
Correct |
36 ms |
37224 KB |
Output is correct |
31 |
Correct |
24 ms |
37224 KB |
Output is correct |
32 |
Correct |
228 ms |
37224 KB |
Output is correct |
33 |
Correct |
156 ms |
37224 KB |
Output is correct |
34 |
Correct |
336 ms |
37224 KB |
Output is correct |
35 |
Correct |
328 ms |
37224 KB |
Output is correct |
36 |
Correct |
280 ms |
37224 KB |
Output is correct |
37 |
Correct |
384 ms |
37224 KB |
Output is correct |
38 |
Correct |
220 ms |
37224 KB |
Output is correct |
39 |
Correct |
392 ms |
37224 KB |
Output is correct |
40 |
Correct |
388 ms |
37224 KB |
Output is correct |