제출 #119979

#제출 시각아이디문제언어결과실행 시간메모리
119979tutisGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
15 / 100
9 ms1280 KiB
/*input
20
YYGYYYGGGGRGYYGRGRYG
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
map<tuple<int, int, int, int>, int>M;
int dp(bitset<400>s[3], int x, int n)
{
	int h1 = hash<bitset<400>>()(s[0]);
	int h2 = hash<bitset<400>>()(s[1]);
	int h3 = hash<bitset<400>>()(s[2]);
	if (M.count(make_tuple(h1, h2, h3, x)))
		return M[make_tuple(h1, h2, h3, x)];
	if (n == 1)
	{
		if (s[x][0])
			return 0;
		else
			return 3e8;
	}
	for (int i = n - 1; i >= 0; i--)
	{
		if (s[x][i])
		{
			bitset<400>ss[3] = {s[0], s[1], s[2]};
			ss[x][i] = 0;
			for (int j = i; j + 1 < n; j++)
			{
				ss[0][j] = ss[0][j + 1];
				ss[1][j] = ss[1][j + 1];
				ss[2][j] = ss[2][j + 1];
			}
			int cost = (n - 1) - i;
			int mini = 3e8;
			for (int y = 0; y < 3; y++)
			{
				if (x != y)
				{
					mini = min(mini, dp(ss, y, n - 1));
				}
			}
			return M[make_tuple(h1, h2, h3, x)] = cost + mini;
		}
	}
	return M[make_tuple(h1, h2, h3, x)] = 3e8;
}
int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	string s;
	cin >> n >> s;
	bitset<400>ss[3];
	for (int i = 0; i < n; i++)
	{
		if (s[i] == 'R')
			ss[0][i] = true;
		if (s[i] == 'G')
			ss[1][i] = true;
		if (s[i] == 'Y')
			ss[2][i] = true;
	}
	int ans = min({dp(ss, 0, n), dp(ss, 1, n), dp(ss, 2, n)});
	if (ans > 1e8)
		ans = -1;
	cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...