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>
typedef long long ll;
#define pb push_back
#define MS(x,y) memset((x),(y),sizeof((x)))
#define MN 1000000007
using namespace std;
const int MAXN = 401;
vector<int> pos[3];
string org;
int pref[3][MAXN];
int n;
int dp[3][MAXN][MAXN][MAXN];
int convI(char c) {
if (c == 'R') return 0;
else if (c == 'G') return 1;
else if (c == 'Y') return 2;
}
char convC(int b) {
if (b == 0) return 'R';
else if (b == 1) return 'G';
else if (b == 2) return 'Y';
}
int kolkuPrefLast(int cnt, int koj) {
if (cnt == -1) return 0;
else return pref[koj][pos[koj][cnt]];
}
int rec(int last, int cntR, int cntG, int cntY) {
if (cntR+cntG+cntY == n) return 0;
if (dp[last][cntR][cntG][cntY] != -1) return dp[last][cntR][cntG][cntY];
int ret = INT_MAX/2;
int tmp[3] = {cntR,cntG,cntY};
for (int i = 0; i<3; i++) {
if (last == i && cntR+cntG+cntY != 0) continue;
if (tmp[i] >= pos[i].size()) continue;
int ctr = 0;
for (int j = 0; j<3; j++) {
if (i == j) continue;
ctr += abs(pref[j][pos[i][tmp[i]]] - kolkuPrefLast(tmp[j]-1,j));
}
if (i == 0) ctr += rec(0,cntR+1,cntG,cntY);
else if (i == 1) ctr += rec(1,cntR,cntG+1,cntY);
else if (i == 2) ctr += rec(2,cntR,cntG,cntY+1);
ret = min(ret,ctr);
}
return dp[last][cntR][cntG][cntY] = ret;
}
int main()
{
#if LOCAL_DEBUG
fstream cin("in.txt");
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
MS(dp,-1);
MS(pref,0);
cin>>n;
cin>>org;
for (int i = 0; i<n; i++) {
for (int j = 0; j<3; j++) {
if (i != 0) pref[j][i] = pref[j][i-1];
if (org[i] == convC(j)) {
pref[j][i]++;
pos[j].pb(i);
}
}
}
int res = rec(0,0,0,0);
if (res == INT_MAX/2) cout<<-1<<endl;
else cout<<res/2<<endl;
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int rec(int, int, int, int)':
joi2019_ho_t3.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if (tmp[i] >= pos[i].size()) continue;
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int convI(char)':
joi2019_ho_t3.cpp:22:1: warning: control reaches end of non-void function [-Wreturn-type]
22 | }
| ^
joi2019_ho_t3.cpp: In function 'char convC(int)':
joi2019_ho_t3.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
28 | }
| ^
# | 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... |