Submission #429121

# Submission time Handle Problem Language Result Execution time Memory
429121 2021-06-15T17:36:19 Z Berted Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
236 ms 7876 KB
#include <iostream>
#include <vector>
#define ll long long

using namespace std;

const ll INF = 1e18;

int N, cnt[3][3][401]; string S;

vector<int> A[3];
ll DP[2][3][401][401];

int main()
{
	cin >> N >> S;
	for (int i = 1; i <= N; i++)
	{
		if (S[i - 1] == 'R') 
		{
			A[0].push_back(i);
			for (int j = 0; j < 3; j++) cnt[0][j][A[0].size() - 1] = A[j].size();
		}
		else if (S[i - 1] == 'G') 
		{
			A[1].push_back(i);
			for (int j = 0; j < 3; j++) cnt[1][j][A[1].size() - 1] = A[j].size();
		}
		else 
		{
			A[2].push_back(i);
			for (int j = 0; j < 3; j++) cnt[2][j][A[2].size() - 1] = A[j].size();
		}
	}

	for (int s = 0; s < 3; s++)
		for (int i = 0; i <= N; i++)
			for (int j = 0; j <= N; j++) DP[0][s][i][j] = INF;

	for (int s = 0; s < 3; s++) DP[0][s][0][0] = 0;
	for (int i = 1; i <= N; i++)
	{
		for (int s = 0; s < 3; s++)
			for (int j = 0; j <= N; j++)
				for (int k = 0; k <= N; k++) DP[i & 1][s][j][k] = INF;

		for (int s = 0; s < 3; s++)
		{
			for (int j = 0; j <= i && j <= A[0].size(); j++)
			{
				for (int k = 0; k + j <= i && k <= A[1].size(); k++)
				{
					int l = i - j - k;
					if (l > A[2].size()) continue;

					if (s == 0 && j) 
					{
						for (int t = 0; t < 3; t++)
						{
							if (s == t) continue;
							DP[i & 1][s][j][k] = min(DP[i & 1][s][j][k], DP[~i & 1][t][j - 1][k] + max(0, cnt[s][1][j - 1] - k) + max(0, cnt[s][2][j - 1] - l));
						}
					}

					if (s == 1 && k)
					{
						for (int t = 0; t < 3; t++)
						{
							if (s == t) continue;
							DP[i & 1][s][j][k] = min(DP[i & 1][s][j][k], DP[~i & 1][t][j][k - 1] + max(0, cnt[s][0][k - 1] - j) + max(0, cnt[s][2][k - 1] - l));
						}
					}

					if (s == 2 && l)
					{
						for (int t = 0; t < 3; t++)
						{
							if (s == t) continue;
							DP[i & 1][s][j][k] = min(DP[i & 1][s][j][k], DP[~i & 1][t][j][k] + max(0, cnt[s][0][l - 1] - j) + max(0, cnt[s][1][l - 1] - k));
						}
					}

					//if (DP[i & 1][s][j][k] < INF) cout << i << " " << s << " " << j << " " << k << " " << l << " " << DP[i & 1][s][j][k] << "\n";
				}
			}
		}
	}

	ll res = INF;
	for (int s = 0; s < 3; s++) {res = min(res, DP[N & 1][s][A[0].size()][A[1].size()]);}

	if (res == INF) cout << "-1\n";
	else 
	{
		cout << res << "\n";
	}
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:49:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |    for (int j = 0; j <= i && j <= A[0].size(); j++)
      |                              ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:51:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int k = 0; k + j <= i && k <= A[1].size(); k++)
      |                                   ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:54:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |      if (l > A[2].size()) continue;
      |          ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 560 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 560 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 2 ms 1484 KB Output is correct
19 Correct 3 ms 1484 KB Output is correct
20 Correct 3 ms 1484 KB Output is correct
21 Correct 2 ms 1484 KB Output is correct
22 Correct 2 ms 1356 KB Output is correct
23 Correct 2 ms 1460 KB Output is correct
24 Correct 2 ms 1484 KB Output is correct
25 Correct 2 ms 1484 KB Output is correct
26 Correct 2 ms 1484 KB Output is correct
27 Correct 2 ms 1356 KB Output is correct
28 Correct 2 ms 1356 KB Output is correct
29 Correct 2 ms 1484 KB Output is correct
30 Correct 2 ms 1484 KB Output is correct
31 Correct 2 ms 1484 KB Output is correct
32 Correct 2 ms 1484 KB Output is correct
33 Correct 2 ms 1356 KB Output is correct
34 Correct 2 ms 1356 KB Output is correct
35 Correct 2 ms 1356 KB Output is correct
36 Correct 2 ms 1356 KB Output is correct
37 Correct 2 ms 1356 KB Output is correct
38 Correct 2 ms 1484 KB Output is correct
39 Correct 2 ms 1456 KB Output is correct
40 Correct 2 ms 1460 KB Output is correct
41 Correct 3 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 226 ms 7844 KB Output is correct
3 Correct 177 ms 7876 KB Output is correct
4 Correct 192 ms 7840 KB Output is correct
5 Correct 197 ms 7756 KB Output is correct
6 Correct 180 ms 7840 KB Output is correct
7 Correct 205 ms 7876 KB Output is correct
8 Correct 174 ms 7856 KB Output is correct
9 Correct 177 ms 7828 KB Output is correct
10 Correct 236 ms 7840 KB Output is correct
11 Correct 183 ms 7848 KB Output is correct
12 Correct 28 ms 4172 KB Output is correct
13 Correct 69 ms 5488 KB Output is correct
14 Correct 133 ms 6532 KB Output is correct
15 Correct 194 ms 7756 KB Output is correct
16 Correct 188 ms 7876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 560 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 2 ms 1484 KB Output is correct
19 Correct 3 ms 1484 KB Output is correct
20 Correct 3 ms 1484 KB Output is correct
21 Correct 2 ms 1484 KB Output is correct
22 Correct 2 ms 1356 KB Output is correct
23 Correct 2 ms 1460 KB Output is correct
24 Correct 2 ms 1484 KB Output is correct
25 Correct 2 ms 1484 KB Output is correct
26 Correct 2 ms 1484 KB Output is correct
27 Correct 2 ms 1356 KB Output is correct
28 Correct 2 ms 1356 KB Output is correct
29 Correct 2 ms 1484 KB Output is correct
30 Correct 2 ms 1484 KB Output is correct
31 Correct 2 ms 1484 KB Output is correct
32 Correct 2 ms 1484 KB Output is correct
33 Correct 2 ms 1356 KB Output is correct
34 Correct 2 ms 1356 KB Output is correct
35 Correct 2 ms 1356 KB Output is correct
36 Correct 2 ms 1356 KB Output is correct
37 Correct 2 ms 1356 KB Output is correct
38 Correct 2 ms 1484 KB Output is correct
39 Correct 2 ms 1456 KB Output is correct
40 Correct 2 ms 1460 KB Output is correct
41 Correct 3 ms 1356 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 226 ms 7844 KB Output is correct
44 Correct 177 ms 7876 KB Output is correct
45 Correct 192 ms 7840 KB Output is correct
46 Correct 197 ms 7756 KB Output is correct
47 Correct 180 ms 7840 KB Output is correct
48 Correct 205 ms 7876 KB Output is correct
49 Correct 174 ms 7856 KB Output is correct
50 Correct 177 ms 7828 KB Output is correct
51 Correct 236 ms 7840 KB Output is correct
52 Correct 183 ms 7848 KB Output is correct
53 Correct 28 ms 4172 KB Output is correct
54 Correct 69 ms 5488 KB Output is correct
55 Correct 133 ms 6532 KB Output is correct
56 Correct 194 ms 7756 KB Output is correct
57 Correct 188 ms 7876 KB Output is correct
58 Correct 223 ms 7756 KB Output is correct
59 Correct 203 ms 7844 KB Output is correct
60 Correct 195 ms 7844 KB Output is correct
61 Correct 213 ms 7756 KB Output is correct
62 Correct 206 ms 7756 KB Output is correct
63 Correct 184 ms 7856 KB Output is correct
64 Correct 191 ms 7860 KB Output is correct
65 Correct 210 ms 7756 KB Output is correct
66 Correct 196 ms 7756 KB Output is correct
67 Correct 203 ms 7840 KB Output is correct
68 Correct 214 ms 7844 KB Output is correct
69 Correct 225 ms 7844 KB Output is correct
70 Correct 210 ms 7844 KB Output is correct
71 Correct 205 ms 7756 KB Output is correct
72 Correct 171 ms 7844 KB Output is correct
73 Correct 146 ms 7756 KB Output is correct