This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*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<1202>, int>M;
int dp(bitset<1202>s, int x, int n)
{
s[1200] = x % 2;
s[1201] = x / 2;
if (n == 1)
{
if (s[x * 400])
return 0;
else
return 3e8;
}
if (M.count(s))
return M[s];
for (int i = n - 1; i >= 0; i--)
{
if (s[x * 400 + i])
{
bitset<1202>ss = s;
ss[x * 400 + i] = 0;
for (int j = i; j + 1 < n; j++)
{
ss[j] = ss[j + 1];
ss[400 + j] = ss[400 + j + 1];
ss[800 + j] = ss[800 + 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[s] = cost + mini;
}
}
return M[s] = 3e8;
}
int main()
{
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
bitset<1202>ss;
for (int i = 0; i < n; i++)
{
if (s[i] == 'R')
ss[i] = true;
if (s[i] == 'G')
ss[400 + i] = true;
if (s[i] == 'Y')
ss[800 + 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 time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |