Submission #515786

# Submission time Handle Problem Language Result Execution time Memory
515786 2022-01-19T16:33:06 Z thiago_bastos Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
68 ms 162972 KB
#include "bits/stdc++.h"

using namespace std;
 
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;

const int N = 401, INF = 1e9;

int dp[N][N][N][3];

void solve() {
	
	int n, r, g, y;
	string s, p = "RGY";
	vector<int> pos[3], cnt[3][3];

	cin >> n >> s;

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < 3; ++j) {
			if(s[i] == p[j])
				pos[j].push_back(i);
		}
	}

	r = pos[0].size();
	g = pos[1].size();
	y = pos[2].size();

	for(int i = 0; i <= r; ++i)
	for(int j = 0; j <= g; ++j)
	for(int k = 0; k <= y; ++k)
	for(int l = 0; l < 3; ++l)
		dp[i][j][k][l] = INF;

	for(int i = 0; i < 3; ++i) {
		int n = pos[i].size();
		for(int j = 0; j < 3; ++j) {
			cnt[i][j].resize(n);
			for(int k = 0; k < n; ++k)
				cnt[i][j][k] = lower_bound(pos[j].begin(), pos[j].end(), pos[i][k]) - pos[j].begin();
		}
	}

	for(int l = 0; l < 3; ++l) dp[0][0][0][l] = 0;

	for(int i = 0; i <= r; ++i) {

		for(int j = 0; j <= g; ++j) {

			for(int k = 0; k <= y; ++k) {

				if(i > 0) {
					int moves = max(0, cnt[0][1][i - 1] - j) + max(0, cnt[0][2][i - 1] - k);
					dp[i][j][k][0] = min(dp[i][j][k][0], min(dp[i - 1][j][k][1], dp[i - 1][j][k][2]) + moves);
				}

				if(j > 0) {
					int moves = max(0, cnt[1][0][j - 1] - i) + max(0, cnt[1][2][j - 1] - k);
					dp[i][j][k][1] = min(dp[i][j][k][1], min(dp[i][j - 1][k][0], dp[i][j - 1][k][2]) + moves);
				}

				if(k > 0) {
					int moves = max(0, cnt[2][0][k - 1] - i) + max(0, cnt[2][1][k - 1] - j);
					dp[i][j][k][2] = min(dp[i][j][k][2], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + moves);
				}
			}
		}
	}

	int ans = min({dp[r][g][y][0], dp[r][g][y][1], dp[r][g][y][2]});
	if(ans == INF) ans = -1;
	cout << ans << '\n';
}
 
int main() {
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 0 ms 460 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 460 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 0 ms 316 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 0 ms 460 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 460 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 0 ms 316 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 2252 KB Output is correct
19 Correct 1 ms 1868 KB Output is correct
20 Correct 1 ms 2252 KB Output is correct
21 Correct 1 ms 2380 KB Output is correct
22 Correct 1 ms 1612 KB Output is correct
23 Correct 2 ms 2124 KB Output is correct
24 Correct 1 ms 1612 KB Output is correct
25 Correct 2 ms 3916 KB Output is correct
26 Correct 2 ms 4044 KB Output is correct
27 Correct 1 ms 3020 KB Output is correct
28 Correct 1 ms 2252 KB Output is correct
29 Correct 1 ms 2124 KB Output is correct
30 Correct 1 ms 2252 KB Output is correct
31 Correct 1 ms 1996 KB Output is correct
32 Correct 1 ms 2380 KB Output is correct
33 Correct 2 ms 3788 KB Output is correct
34 Correct 2 ms 3532 KB Output is correct
35 Correct 1 ms 2636 KB Output is correct
36 Correct 1 ms 1868 KB Output is correct
37 Correct 1 ms 1612 KB Output is correct
38 Correct 1 ms 2124 KB Output is correct
39 Correct 1 ms 2252 KB Output is correct
40 Correct 0 ms 332 KB Output is correct
41 Correct 1 ms 2124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 57 ms 162940 KB Output is correct
3 Correct 57 ms 162092 KB Output is correct
4 Correct 59 ms 162908 KB Output is correct
5 Correct 57 ms 162884 KB Output is correct
6 Correct 60 ms 162928 KB Output is correct
7 Correct 63 ms 162028 KB Output is correct
8 Correct 57 ms 162100 KB Output is correct
9 Correct 57 ms 161336 KB Output is correct
10 Correct 57 ms 162972 KB Output is correct
11 Correct 56 ms 162884 KB Output is correct
12 Correct 16 ms 43980 KB Output is correct
13 Correct 27 ms 77124 KB Output is correct
14 Correct 40 ms 111328 KB Output is correct
15 Correct 59 ms 162856 KB Output is correct
16 Correct 57 ms 162956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 0 ms 460 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 460 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 0 ms 316 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 460 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 2252 KB Output is correct
19 Correct 1 ms 1868 KB Output is correct
20 Correct 1 ms 2252 KB Output is correct
21 Correct 1 ms 2380 KB Output is correct
22 Correct 1 ms 1612 KB Output is correct
23 Correct 2 ms 2124 KB Output is correct
24 Correct 1 ms 1612 KB Output is correct
25 Correct 2 ms 3916 KB Output is correct
26 Correct 2 ms 4044 KB Output is correct
27 Correct 1 ms 3020 KB Output is correct
28 Correct 1 ms 2252 KB Output is correct
29 Correct 1 ms 2124 KB Output is correct
30 Correct 1 ms 2252 KB Output is correct
31 Correct 1 ms 1996 KB Output is correct
32 Correct 1 ms 2380 KB Output is correct
33 Correct 2 ms 3788 KB Output is correct
34 Correct 2 ms 3532 KB Output is correct
35 Correct 1 ms 2636 KB Output is correct
36 Correct 1 ms 1868 KB Output is correct
37 Correct 1 ms 1612 KB Output is correct
38 Correct 1 ms 2124 KB Output is correct
39 Correct 1 ms 2252 KB Output is correct
40 Correct 0 ms 332 KB Output is correct
41 Correct 1 ms 2124 KB Output is correct
42 Correct 0 ms 332 KB Output is correct
43 Correct 57 ms 162940 KB Output is correct
44 Correct 57 ms 162092 KB Output is correct
45 Correct 59 ms 162908 KB Output is correct
46 Correct 57 ms 162884 KB Output is correct
47 Correct 60 ms 162928 KB Output is correct
48 Correct 63 ms 162028 KB Output is correct
49 Correct 57 ms 162100 KB Output is correct
50 Correct 57 ms 161336 KB Output is correct
51 Correct 57 ms 162972 KB Output is correct
52 Correct 56 ms 162884 KB Output is correct
53 Correct 16 ms 43980 KB Output is correct
54 Correct 27 ms 77124 KB Output is correct
55 Correct 40 ms 111328 KB Output is correct
56 Correct 59 ms 162856 KB Output is correct
57 Correct 57 ms 162956 KB Output is correct
58 Correct 49 ms 75432 KB Output is correct
59 Correct 55 ms 95624 KB Output is correct
60 Correct 49 ms 86852 KB Output is correct
61 Correct 50 ms 84020 KB Output is correct
62 Correct 61 ms 160512 KB Output is correct
63 Correct 62 ms 160160 KB Output is correct
64 Correct 68 ms 150100 KB Output is correct
65 Correct 65 ms 138420 KB Output is correct
66 Correct 48 ms 85176 KB Output is correct
67 Correct 47 ms 81988 KB Output is correct
68 Correct 50 ms 87776 KB Output is correct
69 Correct 48 ms 84620 KB Output is correct
70 Correct 51 ms 91660 KB Output is correct
71 Correct 49 ms 83100 KB Output is correct
72 Correct 22 ms 41164 KB Output is correct
73 Correct 4 ms 2764 KB Output is correct