This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
template<class T>
bool cmin(T & a,T b) {return b < a ? a = b, 1 : 0;}
const int N = 405;
char s[N];
int n;
int dp[2][N][N][3];
vector<int> d[3];
int pr[N][3];
int f(int i,array<int,3> a)
{
int s = 0;
for (int j = 0; j < 3; ++j)
s += max(0, pr[i][j] - a[j]);
return s - 1;
}
int main()
{
cin >> n >> (s + 1);
d[0] = d[1] = d[2] = {0};
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 3; ++j) {
pr[i][j] += pr[i-1][j];
if ("RGY"[j] == s[i]) {
d[j].push_back(i);
pr[i][j]++;
}
}
}
int na=d[0].size()-1,nb=d[1].size()-1,nc=d[2].size()-1;
memset(dp,0x3f,sizeof dp);
memset(dp[0][0][0],0,3 * sizeof(int));
array<int,3> a = {0,0,0}, b;
int & i = a[0], & j = a[1], & k = a[2];
int & x = b[0], & y = b[1], & z = b[2];
for (int sa = 0; sa < n; ++sa) {
memset(dp[(sa+1)%2],0x3f,sizeof dp[0]);
for (i = 0; i <= min(na, sa); ++i) {
for (j = 0; j <= min(nb, sa - i); ++j) {
k = sa - i - j;
if (k < 0 || k > nc)
continue;
b = a;
for (int c = 0; c < 3; ++c)
for (int u = 0; u < 3; ++u)
if (u != c && b[u] + 1 < d[u].size()) {
b[u]++;
cmin(dp[(sa+1)%2][y][z][u], dp[sa%2][j][k][c] + f(d[u][b[u]],a));
b[u]--;
}
}
}
}
int ans = *min_element(dp[n%2][nb][nc],dp[n%2][nb][nc] + 3);
if (ans == 0x3f3f3f3f)
ans = -1;
cout << ans << endl;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:47:48: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (u != c && b[u] + 1 < d[u].size()) {
joi2019_ho_t3.cpp:36:11: warning: unused variable 'x' [-Wunused-variable]
int & x = b[0], & y = b[1], & z = b[2];
^
# | 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... |