#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define endl '\n'
const ll MOD = 1e9+7;
const ll INF = 1e16;
const ll MAX = 2 * 1e5;
void solve(){
int n; cin>>n;
string temp; cin>>temp;
int r=0,g=0,y=0;
vector<char> arr;
for(char c : temp){
if(c == 'R') r++;
else if(c == 'G') g++;
else y++;
arr.push_back(c);
}
if(max(r, max(g, y)) > (n+1)/2){
cout<<-1;
return;
}
int ans = 0;
for(int i = 2; i < n; i++){
for(int j = 0; j < i-1; j++){
if(arr[j] == arr[j+1] && arr[j] != arr[i]){
ans+= i - j - 1;
char c = arr[i];
arr.erase(next(arr.begin(), i));
arr.insert(next(arr.begin(),j+1), c);
}
}
}
for(int i = n-3; i >= 0; i--){
for(int j = n-1; j > i+1; j--){
if(arr[j] == arr[j-1] && arr[j] != arr[i]){
ans+= j - i - 1;
char c = arr[i];
arr.erase(next(arr.begin(), i));
arr.insert(next(arr.begin(),j-1), c);
}
}
}
cout<<ans;
}
int main()
{
cin.tie(NULL);
ios::sync_with_stdio(NULL);
int t = 1;
//cin >> t;
while(t--){
//cout<<"Case #"<<t<<" > "<<endl;
solve();
cout<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |