Submission #101717

# Submission time Handle Problem Language Result Execution time Memory
101717 2019-03-19T14:51:22 Z KCSC Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
0 / 100
500 ms 780552 KB
#include <bits/stdc++.h>
using namespace std;

const int DIM = 405;
const int INF = 405 * 405;

char str[DIM];
int dp[DIM][DIM][DIM][3], idx[3];
int tot[3], psm[DIM][3], lst[DIM][3];

inline void f(int &x, int y) {
	if (x >= y) { x = y; } }

int main(void) {
#ifdef HOME
	freopen("vegetables.in", "r", stdin);
	freopen("vegetables.out", "w", stdout);
#endif
	int n; cin >> n >> (str + 1);
	for (int i = 1; i <= n; ++i) {
		char ch = str[i];
		if (ch == 'R') { lst[++tot[0]][0] = i; ++psm[i][0]; }
		if (ch == 'G') { lst[++tot[1]][1] = i; ++psm[i][1]; }
		if (ch == 'Y') { lst[++tot[2]][2] = i; ++psm[i][2]; }
		for (int l = 0; l <= 2; ++l) {
			psm[i][l] += psm[i - 1][l]; } }
	memset(dp, 0x3f, sizeof dp);
	for (int l = 0; l <= 2; ++l) {
		dp[0][0][0][l] = 0; }
	for (int a = 0; a <= tot[0]; ++a) {
	for (int b = 0; b <= tot[1]; ++b) {
	for (int c = 0; c <= tot[2]; ++c) {
		for (int l = 0; l <= 2; ++l) {
			if (l != 0 and a < tot[0]) {	
				int p = lst[a + 1][0];
				f(dp[a + 1][b][c][0], dp[a][b][c][l] + max(0, psm[p][1] - b) + max(0, psm[p][2] - c)); }
			
			if (l != 1 and b < tot[1]) {	
				int p = lst[b + 1][1];
				f(dp[a][b + 1][c][1], dp[a][b][c][l] + max(0, psm[p][2] - c) + max(0, psm[p][0] - a)); }
			
			if (l != 2 and c < tot[2]) {	
				int p = lst[c + 1][2];
				f(dp[a][b][c + 1][2], dp[a][b][c][l] + max(0, psm[p][0] - a) + max(0, psm[p][1] - b)); } } } } }
	
	int v = min( { dp[tot[0]][tot[1]][tot[2]][0], dp[tot[0]][tot[1]][tot[2]][1], dp[tot[0]][tot[1]][tot[2]][2] } );
	cout << (v >= INF ? -1 : v) << endl;
	return 0; }
# Verdict Execution time Memory Grader output
1 Execution timed out 692 ms 780552 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 692 ms 780552 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 656 ms 780412 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 692 ms 780552 KB Time limit exceeded
2 Halted 0 ms 0 KB -