This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
/*{{{*/
#include <bits/stdc++.h>
#ifdef LOCAL
int c_ = 0;
#define cout (c_?cerr:cout)
#define debug(...) \
{if(!c_++)cerr<<"\033[96;1m"; \
__VA_ARGS__; \
if(!--c_)cerr<<"\033[0m";}
#else
#define debug(...) {}
#endif
#define assrt(...) debug(\
if (not (__VA_ARGS__)) \
exit((cerr << __LINE__ << ": " << #__VA_ARGS__ << '\n', 0));\
)
#define st first
#define nd second
#define all(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end()
using namespace std; using ll = long long;
template < typename t > using V = vector< t >;
template < typename t, size_t s > using A = array< t, s >;
void print() {cout << '\n';}
template< typename t, typename... v >
void print(const t& a, const v&... b)
{cout << a << (sizeof...(b)?" ":""); print(b...);}
template< typename t > void print_range(t a, const t& b)
{ while (a != b) cout << (*a++) << ' '; cout << '\n';}
#define dump(...) debug(print(#__VA_ARGS__,'=',__VA_ARGS__))
#define dump_range(...) debug(cerr<<#__VA_ARGS__ << ": "; print_range(__VA_ARGS__))
/*}}}*/
using Triple = array< int, 3 >;
constexpr int nax = 400, ax = 200, cx = 3, inf = 1e9;
int n, mem[3][ax + 1][ax + 1][ax + 1], poz[cx][ax + 1], sum[cx][nax + 1];
string s;
int& dp(const int v, const Triple& s)
{
return mem[v][s[0]][s[1]][s[2]];
}
void solve()
{
for (int i = 0; i <= sum[0][n]; ++i)
for (int j = 0; j <= sum[1][n]; ++j)
for (int k = 0; k <= sum[2][n]; ++k)
for (int l = 0; l < cx; ++l)
{
Triple s{i, j, k};
for (int i = 0; i < cx; ++i)
if (s[i] < sum[i][n] and i != l)
{
int p = poz[i][s[i] + 1];
int d = p;
for (int j = 0; j < cx; ++j)
d -= min(sum[j][p], s[j] + (i == j));
int& a = dp(l, s);
++s[i];
int& b = dp(i, s);
--s[i];
b = min(b, a + d);
}
}
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s;
{
int su[3]{};
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < cx; ++j)
sum[j][i] = sum[j][i - 1];
if (s[i - 1] == 'R')
{
if (sum[0][i] == ax)
return print(-1), 0;
++sum[0][i], poz[0][sum[0][i]] = i;
}
if (s[i - 1] == 'Y')
{
if (sum[1][i] == ax)
return print(-1), 0;
++sum[1][i], poz[1][sum[1][i]] = i;
}
if (s[i - 1] == 'G')
{
if (sum[2][i] == ax)
return print(-1), 0;
++sum[2][i], poz[2][sum[2][i]] = i;
}
}
}
for (int i = 0; i <= sum[0][n]; ++i)
for (int j = 0; j <= sum[1][n]; ++j)
for (int k = 0; k <= sum[2][n]; ++k)
for (int l = 0; l < cx; ++l)
mem[l][i][j][k] = inf;
mem[0][0][0][0] = mem[1][0][0][0] = mem[2][0][0][0] = 0;
solve();
int res = inf;
for (int i = 0; i < cx; ++i)
res = min(res, mem[i][sum[0][n]][sum[1][n]][sum[2][n]]);
print((res == inf? -1 : res));
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:77:17: warning: unused variable 'su' [-Wunused-variable]
int su[3]{};
^~
# | 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... |