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;
const int maxn = 400;
const int INF = 1e9;
vector<int> pos[3];
vector<vector<vector<int>>> dp[3];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for(int j = 0;j < n;++j)
{
if(s[j] == 'R')
{
pos[0].push_back(j);
}
else if(s[j] == 'G')
{
pos[1].push_back(j);
}
else
pos[2].push_back(j);
}
for(int i = 0;i < 3;++i)
{
dp[i].resize(pos[0].size()+1);
for(int j= 0;j < dp[i].size();++j)
{
dp[i][j].resize(pos[1].size()+1);
for(int k = 0;k < dp[i][j].size();++k)
{
dp[i][j][k].resize(pos[2].size()+1);
}
}
}
int k[3] = {0};
for(k[0] = 0;k[0] <= pos[0].size();++k[0])
{
for(k[1] = 0;k[1] <= pos[1].size();++k[1])
{
for(k[2] = 0;k[2] <= pos[2].size();++k[2])
{
for(int last = 0;last < 3;++last)
{
auto &cdp = dp[last][k[0]][k[1]][k[2]];
if(k[0]+k[1]+k[2] == 0)
{
cdp = 0;
continue;
}
cdp = INF;
if(k[last] != 0)
{
int cpos = pos[last][k[last]-1];
int ind0 = upper_bound(pos[0].begin(),pos[0].end(),cpos)-pos[0].begin();
int ind1 = upper_bound(pos[1].begin(),pos[1].end(),cpos)-pos[1].begin();
int ind2 = upper_bound(pos[2].begin(),pos[2].end(),cpos)-pos[2].begin();
int c = max(0,k[0]-ind0) + max(0,k[1]-ind1) + max(0,k[2]-ind2);
k[last]--;
for(int nlast = 0;nlast < 3;++nlast)
{
if(nlast != last)
{
cdp = min(cdp,c + dp[nlast][k[0]][k[1]][k[2]]);
}
}
k[last]++;
}
}
}
}
}
int ans = INF;
for(int le = 0;le < 3;++le)
ans = min(ans,dp[le][pos[0].size()][pos[1].size()][pos[2].size()]);
cout << (ans == INF ? -1 : ans);
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:37:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::vector<int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j= 0;j < dp[i].size();++j)
| ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:40:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int k = 0;k < dp[i][j].size();++k)
| ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(k[0] = 0;k[0] <= pos[0].size();++k[0])
| ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(k[1] = 0;k[1] <= pos[1].size();++k[1])
| ~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:51:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(k[2] = 0;k[2] <= pos[2].size();++k[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... |