/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 405, M = 1e5+10, K = 52, MX = 30;
int n, dp[N][N][N][3];
string s, c = "RGY";
int m(char x){
if(x==c[0]) return 0;
if(x==c[1]) return 1;
return 2;
}
void solve(){
cin >> n >> s;
array<int, 3> x = {0, 0, 0};
for(int i = 0 ;i < n; ++i){
x[m(s[i])]++;
}
if(max({x[0], x[1], x[2]}) > (n+1)/2){
cout << -1;
return;
}
vector<int> v[3];
for(int i = 0; i < n; ++i){
v[m(s[i])].pb(i);
}
// for(auto x: v[0]) cout << x << ' '; en;
// for(auto x: v[1]) cout << x << ' '; en;
// for(auto x: v[2]) cout << x << ' '; en;
for(int i = 0; i <= n; ++i) for(int j = 0; j <= n; ++j) for(int l = 0; l <= n; ++l) dp[i][j][l][0] = dp[i][j][l][1] = dp[i][j][l][2] = MOD;
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int i = 1; i <= n; ++i){
// cout << i << endl;
for(int j = 0; j <= min(i, x[0]); ++j){
for(int l = 0; l <= min(i - j, x[1]); ++l){
// cout << j << ' ' << l << endl;
for(int x1 = 0; x1 < 3; ++x1){
// cout << x1 << "x1" << endl;
for(int x2 = 0; x2 < 3; ++x2){
if(x1==x2) continue;
int j1 = j, l1 = l, k1 = i-j-l;
if(x1 == 0) j1--;
if(x1 == 1) l1--;
if(x1 == 2) k1--;
if(min({l1, j1, k1}) < 0) continue;
if((x1 == 0 && v[x1].size() < j) || (x1 == 1 && v[x1].size() < l) || (x1 == 2 && v[x1].size() < i-j-l)) continue;
int cost = v[x1][(x1 == 0 ? j1 : (x1 == 1 ? l1 : k1))];
// cout << "hm " << endl;
dp[i][j][l][x1] = min(dp[i][j][l][x1], dp[i - 1][j1][l1][x2] + abs(cost - (i-1)));
}
}
}
}
}
cout << min({dp[n][x[0]][x[1]][0], dp[n][x[0]][x[1]][1], dp[n][x[0]][x[1]][2]}) /2;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:56:41: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
56 | if((x1 == 0 && v[x1].size() < j) || (x1 == 1 && v[x1].size() < l) || (x1 == 2 && v[x1].size() < i-j-l)) continue;
| ~~~~~~~~~~~~~^~~
joi2019_ho_t3.cpp:56:74: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
56 | if((x1 == 0 && v[x1].size() < j) || (x1 == 1 && v[x1].size() < l) || (x1 == 2 && v[x1].size() < i-j-l)) continue;
| ~~~~~~~~~~~~~^~~
joi2019_ho_t3.cpp:56:107: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
56 | if((x1 == 0 && v[x1].size() < j) || (x1 == 1 && v[x1].size() < l) || (x1 == 2 && v[x1].size() < i-j-l)) continue;
| ~~~~~~~~~~~~~^~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:73:15: warning: unused variable 'aa' [-Wunused-variable]
73 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
31064 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
3 ms |
31064 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Incorrect |
3 ms |
31068 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
31064 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
3 ms |
31064 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Incorrect |
3 ms |
31068 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
343 ms |
774216 KB |
Output is correct |
3 |
Correct |
359 ms |
772264 KB |
Output is correct |
4 |
Correct |
371 ms |
774228 KB |
Output is correct |
5 |
Correct |
338 ms |
774224 KB |
Output is correct |
6 |
Correct |
342 ms |
774208 KB |
Output is correct |
7 |
Correct |
334 ms |
772168 KB |
Output is correct |
8 |
Correct |
329 ms |
772320 KB |
Output is correct |
9 |
Correct |
357 ms |
769872 KB |
Output is correct |
10 |
Correct |
391 ms |
774228 KB |
Output is correct |
11 |
Correct |
336 ms |
773972 KB |
Output is correct |
12 |
Correct |
78 ms |
398672 KB |
Output is correct |
13 |
Correct |
142 ms |
530260 KB |
Output is correct |
14 |
Correct |
205 ms |
638576 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
3 ms |
31064 KB |
Output is correct |
8 |
Correct |
3 ms |
31068 KB |
Output is correct |
9 |
Correct |
3 ms |
31064 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Incorrect |
3 ms |
31068 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |