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;
using ll = long long;
const int INF = 1e9;
int n, dp[444][444][444][3], p[444][3];
string s;
vector<int> r, g, y;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for(int i=0;i<n;i++) {
if(s[i] == 'R') r.push_back(i+1);
if(s[i] == 'G') g.push_back(i+1);
if(s[i] == 'Y') y.push_back(i+1);
p[i+1][0] = r.size();
p[i+1][1] = g.size();
p[i+1][2] = y.size();
}
for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
if(!i && !j && !k) continue;
for(int l=0;l<3;l++) dp[i][j][k][l] = INF;
int u = i+j+k;
if(i) dp[i][j][k][0] = min(dp[i-1][j][k][1], dp[i-1][j][k][2]) + ((r[i-1] + max(0, j-p[r[i-1]][1]) + max(0, k-p[r[i-1]][2])) - u);
if(j) dp[i][j][k][1] = min(dp[i][j-1][k][0], dp[i][j-1][k][2]) + ((g[j-1] + max(0, i-p[g[j-1]][0]) + max(0, k-p[g[j-1]][2])) - u);
if(k) dp[i][j][k][2] = min(dp[i][j][k-1][0], dp[i][j][k-1][1]) + ((y[k-1] + max(0, i-p[y[k-1]][0]) + max(0, j-p[y[k-1]][1])) - u);
}
int ans = *min_element(dp[r.size()][g.size()][y.size()], dp[r.size()][g.size()][y.size()]+3);
cout << (ans >= INF ? -1 : ans);
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:24:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
| ~^~~~~~~~~~
joi2019_ho_t3.cpp:24:76: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<=r.size();i++) for(int j=0;j<=g.size();j++) for(int k=0;k<=y.size();k++) {
| ~^~~~~~~~~~
# | 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... |