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;
#define ll long long
#define endl "\n"
#define all(aa) aa.begin(), aa.end()
const int INF=1e8;
int n;
vector<int> ind[3];
map<pair<array<int, 3>, char>, int> dp;
int f(array<int, 3> x, char l){
if(x[0]+x[1]+x[2]==n) return 0;
if(dp.count({x, l})) return dp[{x, l}];
int minn=INF;
for(int i=0; i<3; i++) if(i!=l && x[i]!=ind[i].size()){
int o=0, s=x[0]+x[1]+x[2];
for(int j=0; j<3; j++) if(j!=i)
o+=max(0, (int)(ind[j].begin()+x[j]-upper_bound(all(ind[j]), ind[i][x[i]])));
x[i]++;
minn=min(minn, f(x, i)+o+ind[i][x[i]-1]-s);
x[i]--;
}
return dp[{x, l}]=minn;
}
int main(){
string s;
cin>>n>>s;
for(int i=0; i<n; i++){
if(s[i]=='R')
ind[0].push_back(i);
else if(s[i]=='G')
ind[1].push_back(i);
else
ind[2].push_back(i);
}
int ans=f(array<int, 3>{0, 0, 0}, 3);
cout<<(ans>=INF ? -1:ans)<<endl;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int f(std::array<int, 3>, char)':
joi2019_ho_t3.cpp:18:40: warning: comparison of integer expressions of different signedness: 'std::array<int, 3>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i=0; i<3; i++) if(i!=l && x[i]!=ind[i].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... |