Submission #398273

# Submission time Handle Problem Language Result Execution time Memory
398273 2021-05-04T04:43:23 Z mars Sequence (BOI14_sequence) C++14
100 / 100
338 ms 36092 KB
#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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 4 ms 588 KB Output is correct
15 Correct 4 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 3 ms 588 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 3 ms 588 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 4 ms 588 KB Output is correct
12 Correct 3 ms 588 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 4 ms 588 KB Output is correct
17 Correct 4 ms 588 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 3 ms 588 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 4 ms 588 KB Output is correct
23 Correct 4 ms 668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 26 ms 4288 KB Output is correct
3 Correct 27 ms 4160 KB Output is correct
4 Correct 28 ms 4160 KB Output is correct
5 Correct 27 ms 4172 KB Output is correct
6 Correct 18 ms 3148 KB Output is correct
7 Correct 174 ms 25132 KB Output is correct
8 Correct 117 ms 16460 KB Output is correct
9 Correct 246 ms 36036 KB Output is correct
10 Correct 281 ms 36088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 154 ms 15824 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 3 ms 588 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 3 ms 588 KB Output is correct
11 Correct 251 ms 36084 KB Output is correct
12 Correct 257 ms 36036 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 3 ms 588 KB Output is correct
15 Correct 3 ms 588 KB Output is correct
16 Correct 4 ms 588 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 4 ms 592 KB Output is correct
20 Correct 4 ms 660 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 4 ms 588 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 4 ms 660 KB Output is correct
26 Correct 4 ms 716 KB Output is correct
27 Correct 27 ms 4200 KB Output is correct
28 Correct 27 ms 4172 KB Output is correct
29 Correct 27 ms 4260 KB Output is correct
30 Correct 27 ms 4288 KB Output is correct
31 Correct 18 ms 3172 KB Output is correct
32 Correct 174 ms 25156 KB Output is correct
33 Correct 115 ms 16472 KB Output is correct
34 Correct 249 ms 36088 KB Output is correct
35 Correct 254 ms 36092 KB Output is correct
36 Correct 250 ms 26060 KB Output is correct
37 Correct 330 ms 35120 KB Output is correct
38 Correct 198 ms 20684 KB Output is correct
39 Correct 338 ms 36088 KB Output is correct
40 Correct 338 ms 36088 KB Output is correct