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>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 410;
const int inf = 100000000;
int dp[maxn][maxn][maxn][3];
int n, arr[maxn], pref[maxn][3];
string code;
vector<int>v[3];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>code;
for(int i=0;i<code.length();i++) {
if(code[i] == 'R') arr[i + 1] = 0;
else if(code[i] == 'G') arr[i + 1] = 1;
else arr[i + 1] = 2;
v[arr[i+1]].pb(i + 1);
for(int j=0;j<=2;j++) {
pref[i+1][j] = pref[i][j];
}
pref[i+1][arr[i+1]]++;
}
for(int i=0;i<=v[0].size()+1;i++) {
for(int j=0;j<=v[1].size()+1;j++) {
for(int k=0;k<=v[2].size()+1;k++) {
for(int l=0;l<=2;l++) {
dp[i][j][k][l] = inf;
}
}
}
}
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for(int c0=0;c0<=v[0].size();c0++) {
for(int c1=0;c1<=v[1].size();c1++) {
for(int c2=0;c2<=v[2].size();c2++) {
for(int last=0;last<=2;last++) {
if(dp[c0][c1][c2][last] == inf) continue;
//cout<<c0<<", "<<c1<<", "<<c2<<", "<<last<<" -> "<<dp[c0][c1][c2][last]<<"\n";
if(last != 0 && c0 < v[0].size()) {
int ind = v[0][c0];
int cost = max(0, pref[ind][1] - c1) + max(0, pref[ind][2] - c2);
//cout<<"Place 0: "<<ind<<" "<<max(0, pref[1][ind] - c1)<<" "<<max(0, pref[2][ind] - c0)<<"\n";
dp[c0+1][c1][c2][0] = min(dp[c0+1][c1][c2][0], dp[c0][c1][c2][last] + cost);
}
if(last != 1 && c1 < v[1].size()) {
int ind = v[1][c1];
int cost = max(0, pref[ind][0] - c0) + max(0, pref[ind][2] - c2);
//cout<<"Place 1: "<<ind<<" "<<max(0, pref[0][ind] - c0)<<" "<<max(0, pref[2][ind] - c2)<<"\n";
dp[c0][c1+1][c2][1] = min(dp[c0][c1+1][c2][1], dp[c0][c1][c2][last] + cost);
}
if(last != 2 && c2 < v[2].size()) {
int ind = v[2][c2];
int cost = max(0, pref[ind][0] - c0) + max(0, pref[ind][1] - c1);
//cout<<"Place 2: "<<ind<<" "<<max(0, pref[1][ind] - c1)<<" "<<max(0, pref[0][ind] - c0)<<"\n";
dp[c0][c1][c2+1][2] = min(dp[c0][c1][c2+1][2], dp[c0][c1][c2][last] + cost);
}
}
}
}
}
int result = min(min(dp[v[0].size()][v[1].size()][v[2].size()][0], dp[v[0].size()][v[1].size()][v[2].size()][1]), dp[v[0].size()][v[1].size()][v[2].size()][2]);
if(result == inf) {
cout<<"-1\n";
return 0;
}
cout<<result<<"\n";
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:20:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<code.length();i++) {
~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:32:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<=v[0].size()+1;i++) {
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:33:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<=v[1].size()+1;j++) {
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:34:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<=v[2].size()+1;k++) {
~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:44:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int c0=0;c0<=v[0].size();c0++) {
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int c1=0;c1<=v[1].size();c1++) {
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:46:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int c2=0;c2<=v[2].size();c2++) {
~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:52:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(last != 0 && c0 < v[0].size()) {
~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:58:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(last != 1 && c1 < v[1].size()) {
~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:64:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(last != 2 && c2 < v[2].size()) {
~~~^~~~~~~~~~~~~
# | 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... |