// #pragma GCC optimize("O3")
// #pragma GCC optimization("Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>
const ll mod = 1e9 + 9;
const int MAXN = 400 + 2;
const ll oo = 1e9 + 7;
const int base = 10;
int n;
string s;
vector<int> pos[3];
int f[MAXN][MAXN][MAXN][3];
int dp(int j, int k, int t, int last){
int i=j+k+t;
if(i>n-1){
return 0;
}
if(f[j][k][t][last]!=-1){
return f[j][k][t][last];
}
int ans=oo;
if(i==0 || last!=0){
if(j<=(int)pos[0].size()-1){
ans=min(ans, dp(j+1, k, t, 0)+abs(pos[0][j]-i));
}
}
if(i==0 || last!=1){
if(k<=(int)pos[1].size()-1){
ans=min(ans, dp(j, k+1, t, 1)+abs(pos[1][k]-i));
}
}
if(i==0 || last!=2){
if(t<=(int)pos[2].size()-1){
ans=min(ans, dp(j, k, t+1, 2)+abs(pos[2][t]-i));
}
}
return f[j][k][t][last]=ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("test.txt", "r", stdin);
// freopen("o2.out", "w", stdout);
if(fopen(".inp", "r")){
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> s;
FOR(i, 0, n-1){
if(s[i]=='R'){
pos[0].push_back(i);
}
else if(s[i]=='G'){
pos[1].push_back(i);
}
else{
pos[2].push_back(i);
}
}
FOR(j, 0, (int)pos[0].size()+1){
FOR(k, 0, (int)pos[1].size()+1){
FOR(t, 0, (int)pos[2].size()+1){
FOR(last, 0, 2){
f[j][k][t][last]=-1;
}
}
}
}
int ans=dp(0, 0, 0, 0);
if(ans==oo){
ans=-2;
}
cout << ans/2;
// cout << f[0][0][0][0] << ' ';
return 0;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:63:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:64:18: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |