제출 #119984

#제출 시각아이디문제언어결과실행 시간메모리
119984tutisGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
75 / 100
1065 ms80132 KiB
/*input
20
YYGYYYGGGGRGYYGRGRYG
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
unordered_map<bitset<1203>, int>M;
int dp(bitset<1203>s, int x, int n)
{
	s[1200] = (x == 0);
	s[1201] = (x == 1);
	s[1202] = (x == 2);
	if (n == 1)
	{
		if (s[x * 400])
			return 0;
		else
			return 1e8 + 5;
	}
	if (M.count(s))
		return M[s];
	for (int i = 0; i < n; i++)
	{
		if (s[x * 400 + i])
		{
			bitset<1203>ss = s;
			ss[x * 400 + i] = false;
			for (int j = i; j + 1 < n; j++)
			{
				ss[000 + j] = ss[000 + j + 1];
				ss[400 + j] = ss[400 + j + 1];
				ss[800 + j] = ss[800 + j + 1];
			}
			for (int y = 0; y <= 2; y++)
				ss[400 * y + n - 1] = false;
			int cost = i;
			int mini = 3e8;
			for (int y = 0; y <= 2; y++)
			{
				if (x != y)
				{
					mini = min(mini, dp(ss, y, n - 1));
				}
			}
			return M[s] = cost + mini;
		}
	}
	return M[s] = 3e8;
}
int code(char c)
{
	if (c == 'R')
		return 0;
	if (c == 'G')
		return 1;
	return 2;
}
int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	string s;
	cin >> n >> s;
	bitset<1203>ss;
	for (int i = 0; i < n; i++)
	{
		ss[400 * code(s[i]) + 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...