Submission #166943

# Submission time Handle Problem Language Result Execution time Memory
166943 2019-12-04T18:05:26 Z johutha Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
294 ms 134788 KB
#include <vector>
#include <iostream>
#include <algorithm>

#define int long long

using namespace std;

struct prefsum
{
	int n;
	vector<int> vals;

	void init(vector<int> ip)
	{
		n = ip.size();
		vals.resize(n + 1);
		for (int i = 1; i <= n; i++)
		{
			vals[i] = vals[i - 1] + ip[i - 1];
		}
	}

	int operator()(int l, int r)
	{
		return vals[r] - vals[l];
	}
};

struct dpstruct
{
	prefsum veg_R;
	prefsum veg_Y;
	prefsum veg_G;

	vector<int> pos_R;
	vector<int> pos_Y;
	vector<int> pos_G;

	int R = 0, Y = 0, G = 0;

	vector<vector<vector<vector<int>>>> table;

	int dp()
	{
		for (int r = 0; r <= R; r++)
		{
			for (int y = 0; y <= Y; y++)
			{
				for (int g = 0; g <= G; g++)
				{
					for (int last = 0; last < 3; last++)
					{
						int cost = 1e15;

						if (last == 0 && r != 0)
						{
							cost = min(table[r - 1][y][g][1], table[r - 1][y][g][2]) + max(0LL, veg_Y(0, pos_R[r - 1]) - y) + max(0LL, veg_G(0, pos_R[r - 1]) - g);
						}
						else if (last == 1 && y != 0)
						{
							cost = min(table[r][y - 1][g][0], table[r][y - 1][g][2]) + max(0LL, veg_R(0, pos_Y[y - 1]) - r) + max(0LL, veg_G(0, pos_Y[y - 1]) - g);
						}
						else if (last == 2 && g != 0)
						{
							cost = min(table[r][y][g - 1][0], table[r][y][g - 1][1]) + max(0LL, veg_Y(0, pos_G[g - 1]) - y) + max(0LL, veg_R(0, pos_G[g - 1]) - r);
						}
						if (r + y + g != 0) table[r][y][g][last] = cost;
					}
				}
			}
		}

		return min(table[R][Y][G][0], min(table[R][Y][G][1], table[R][Y][G][2]));
	}

	void init(int n, string s)
	{
		vector<int> rs(n), ys(n), gs(n);
		
		
		for (int i = 0; i < n; i++)
		{
			char c = s[i];
			if (c == 'R')
			{
				R++;
				rs[i]++;
				pos_R.push_back(i);
			}
			else if (c == 'Y')
			{
				Y++;
				ys[i]++;
				pos_Y.push_back(i);
			}
			else
			{
				G++;
				gs[i]++;
				pos_G.push_back(i);
			}
		}

		veg_R.init(rs);
		veg_Y.init(ys);
		veg_G.init(gs);

		table.resize(R + 1, vector<vector<vector<int>>>(Y + 1, vector<vector<int>>(G + 1, vector<int>(3, 0))));
	}
};

signed main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	string s;
	cin >> s;
	dpstruct dps;
	dps.init(n, s);

	int res = dps.dp();

	if (res >= 1e15) cout << "-1\n";
	else cout << res << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 248 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 248 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 3 ms 888 KB Output is correct
19 Correct 3 ms 888 KB Output is correct
20 Correct 3 ms 888 KB Output is correct
21 Correct 3 ms 888 KB Output is correct
22 Correct 3 ms 888 KB Output is correct
23 Correct 3 ms 888 KB Output is correct
24 Correct 3 ms 760 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 3 ms 760 KB Output is correct
28 Correct 3 ms 888 KB Output is correct
29 Correct 3 ms 888 KB Output is correct
30 Correct 3 ms 888 KB Output is correct
31 Correct 3 ms 888 KB Output is correct
32 Correct 3 ms 888 KB Output is correct
33 Correct 2 ms 504 KB Output is correct
34 Correct 2 ms 632 KB Output is correct
35 Correct 3 ms 760 KB Output is correct
36 Correct 3 ms 888 KB Output is correct
37 Correct 3 ms 760 KB Output is correct
38 Correct 3 ms 888 KB Output is correct
39 Correct 3 ms 888 KB Output is correct
40 Correct 2 ms 504 KB Output is correct
41 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 6 ms 2552 KB Output is correct
3 Correct 7 ms 2552 KB Output is correct
4 Correct 7 ms 2552 KB Output is correct
5 Correct 7 ms 2552 KB Output is correct
6 Correct 6 ms 2552 KB Output is correct
7 Correct 6 ms 2808 KB Output is correct
8 Correct 6 ms 2552 KB Output is correct
9 Correct 7 ms 2592 KB Output is correct
10 Correct 6 ms 2680 KB Output is correct
11 Correct 7 ms 2552 KB Output is correct
12 Correct 3 ms 888 KB Output is correct
13 Correct 4 ms 1400 KB Output is correct
14 Correct 5 ms 1912 KB Output is correct
15 Correct 7 ms 2552 KB Output is correct
16 Correct 6 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 248 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 3 ms 888 KB Output is correct
19 Correct 3 ms 888 KB Output is correct
20 Correct 3 ms 888 KB Output is correct
21 Correct 3 ms 888 KB Output is correct
22 Correct 3 ms 888 KB Output is correct
23 Correct 3 ms 888 KB Output is correct
24 Correct 3 ms 760 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 3 ms 760 KB Output is correct
28 Correct 3 ms 888 KB Output is correct
29 Correct 3 ms 888 KB Output is correct
30 Correct 3 ms 888 KB Output is correct
31 Correct 3 ms 888 KB Output is correct
32 Correct 3 ms 888 KB Output is correct
33 Correct 2 ms 504 KB Output is correct
34 Correct 2 ms 632 KB Output is correct
35 Correct 3 ms 760 KB Output is correct
36 Correct 3 ms 888 KB Output is correct
37 Correct 3 ms 760 KB Output is correct
38 Correct 3 ms 888 KB Output is correct
39 Correct 3 ms 888 KB Output is correct
40 Correct 2 ms 504 KB Output is correct
41 Correct 2 ms 504 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 6 ms 2552 KB Output is correct
44 Correct 7 ms 2552 KB Output is correct
45 Correct 7 ms 2552 KB Output is correct
46 Correct 7 ms 2552 KB Output is correct
47 Correct 6 ms 2552 KB Output is correct
48 Correct 6 ms 2808 KB Output is correct
49 Correct 6 ms 2552 KB Output is correct
50 Correct 7 ms 2592 KB Output is correct
51 Correct 6 ms 2680 KB Output is correct
52 Correct 7 ms 2552 KB Output is correct
53 Correct 3 ms 888 KB Output is correct
54 Correct 4 ms 1400 KB Output is correct
55 Correct 5 ms 1912 KB Output is correct
56 Correct 7 ms 2552 KB Output is correct
57 Correct 6 ms 2680 KB Output is correct
58 Correct 291 ms 133536 KB Output is correct
59 Correct 289 ms 133368 KB Output is correct
60 Correct 289 ms 132380 KB Output is correct
61 Correct 291 ms 133880 KB Output is correct
62 Correct 26 ms 11256 KB Output is correct
63 Correct 44 ms 19704 KB Output is correct
64 Correct 144 ms 65916 KB Output is correct
65 Correct 206 ms 94840 KB Output is correct
66 Correct 289 ms 131852 KB Output is correct
67 Correct 288 ms 131808 KB Output is correct
68 Correct 291 ms 133684 KB Output is correct
69 Correct 294 ms 134788 KB Output is correct
70 Correct 291 ms 134308 KB Output is correct
71 Correct 288 ms 131320 KB Output is correct
72 Correct 86 ms 34680 KB Output is correct
73 Correct 27 ms 10744 KB Output is correct