#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
const int INF = 1e9;
template<class T>void minimize(T& a, T b){
if(a > b){
a = b;
}
}
int n;
string s;
namespace sub1{
void solve(){
unordered_map<string, int>f;
f[s] = 1;
queue<string>q;
q.push(s);
while(!q.empty()){
s = q.front();
q.pop();
bool flag = true;
for(int i = 1; i < n; i++){
if(s[i] == s[i - 1]){
flag = false;
break;
}
}
if(flag){
return void(cout << f[s] - 1);
}
int N = f[s];
for(int i = 1; i < n; i++){
swap(s[i], s[i - 1]);
if(f[s] == 0){
f[s] = N + 1;
q.push(s);
}
swap(s[i], s[i - 1]);
}
}
cout << -1;
}
}
namespace sub3{
void solve(){
int R = count(s.begin(), s.end(), 'R'), G = n - R;
if(abs(R - G) > 1){
return void(cout << -1);
}
if(R == G){
int ans_1 = 0, ans_2 = 0;
for(int i = 0, r = 0, g = 0; i < n; i++){
if(s[i] == 'R'){
ans_1 += abs(i - r);
r += 2;
}
else{
ans_2 += abs(i - g);
g += 2;
}
}
cout << min(ans_1, ans_2);
}
else if(R == G + 1){
int ans = 0;
for(int i = 0, j = 0; i < n; i++){
if(s[i] == 'R'){
ans += abs(i - j);
j += 2;
}
}
cout << ans;
}
else{
int ans = 0;
for(int i = 0, j = 0; i < n; i++){
if(s[i] == 'G'){
ans += abs(i - j);
j += 2;
}
}
cout << ans;
}
}
}
namespace sub24{
const int lim = 405;
int cnt_upper[3][lim][lim], dp[2][lim][lim][3];
vector<int>pos[3];
int get_id(char c){
return c == 'R' ? 0 : (c == 'G' ? 1 : 2);
}
void solve(){
for(int i = 0; i < n; i++){
pos[get_id(s[i])].emplace_back(i + 1);
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < pos[i].size(); j++){
for(int k = 1; k <= n; k++){
cnt_upper[i][j][k] = max(0, j - int(upper_bound(pos[i].begin(), pos[i].end(), k) - pos[i].begin()) + 1);
}
}
}
memset(dp, 0x3f, sizeof(dp));
bool cur = true, pre = false;
for(int i = 0; i < 3; i++){
if(!pos[i].empty()){
dp[cur][int(pos[0].size()) - int(i == 0)][int(pos[1].size()) - int(i == 1)][i] = pos[i][0] - 1;
}
}
for(int i = 2; i <= n; i++){
swap(cur, pre);
for(int j = 0; j < lim; j++){
for(int k = 0; k < lim; k++){
dp[cur][j][k][0] = dp[cur][j][k][1] = dp[cur][j][k][2] = INF;
}
}
for(int r = 0; r <= pos[0].size(); r++){
for(int g = 0; g <= pos[1].size(); g++){
int y = n - i + 1 - r - g;
if(y > -1 && y <= pos[2].size()){
vector<int>cnt = {r, g, y};
for(int j = 0; j < 3; j++){
if(cnt[j] > 0){
for(int k = 0; k < 3; k++){
if(k != j){
int p = pos[j][int(pos[j].size()) - cnt[j]];
minimize(dp[cur][cnt[0] - int(j == 0)][cnt[1] - int(j == 1)][j], dp[pre][cnt[0]][cnt[1]][k] + abs(i - (p + cnt_upper[0][int(pos[0].size()) - cnt[0] - 1][p] + cnt_upper[1][int(pos[1].size()) - cnt[1] - 1][p] + cnt_upper[2][int(pos[2].size()) - cnt[2] - 1][p])));
}
}
}
}
}
}
}
}
int ans = min(dp[cur][0][0][0], min(dp[cur][0][0][1], dp[cur][0][0][2]));
cout << (ans < INF ? ans : -1);
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> s;
if(n <= 15){
sub24::solve();
}
else if(find(s.begin(), s.end(), 'Y') == s.end()){
sub3::solve();
}
else{
sub24::solve();
}
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:144:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |