#include <bits/stdc++.h>
#define endl "\n"
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz size()
#define rep(i,k,n) for(int i = k ; i <= n ; ++i)
#define per(i,k,n) for(int i = k ; i >= n ; --i)
#define Zymraq() ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(x) x.begin(),x.end()
#define fr(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define toqta return 0
#define PERMUTE next_permutation
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
#define a() a + 1, a + n + 1
using namespace std;
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse,-fgcse-lm")
#pragma GCC optimize("-ftree-pre,-ftree-vrp")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef unsigned long long ull;
typedef string S;
typedef double ld;
typedef long double lld;
const int N = 1e6 + 17;
const int modd = 1e9 + 7;
const ll INF = 1e18 - 19;
const int P = 37;
const ll NN = 1e7 + 17;
const ld eps = 1e-19;
const double pi = 3.141592653589793238462643383279 ;
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b){
return (a.second < b.second);
}
void pre (ll a) {
cout << fixed << setprecision(a);
return;
}
ll n, a[N];
ll r, g;
string s;
vector <char> b;
inline void Solution () {
cin >> n;
cin >> s;
for (int i = 0; i < s.size(); ++ i) {
if (s[i] == 'R') r ++;
else g ++;
b.pb (s[i]);
}
if (abs (r - g) > 1) {
cout << -1;
return;
}
vector <char> odd;
vector <char> even;
for (int i = 0; i < s.size(); ++ i) {
if ((i + 1) % 2 != 0) {
odd.pb ('R');
even.pb ('G');
} else {
odd.pb ('G');
even.pb ('R');
}
}
ll mx = 0, cnt = 0;
for (int i = 0; i < odd.size(); ++ i) {
if (odd[i] != b[i]) {
if (i + 1 == odd.size()) {
cnt = INF;
break;
}
ll pr = b[i];
for (int j = i + 1; j < odd.size(); ++ j) {
if (b[j] == odd[i]) {
cnt += j - i;
b[i] = b[j];
b[j] = pr;
break;
}
}
}
}
mx = max (mx, cnt);
cnt = 0;
for (int i = 0; i < even.size(); ++ i) {
if (even[i] != s[i]) {
if (i + 1 == even.size()) {
cnt = INF;
break;
}
ll pr = s[i];
for (int j = i + 1; j < even.size(); ++ j) {
if (s[j] == even[i]) {
cnt += j - i;
s[i] = s[j];
s[j] = pr;
break;
}
}
}
}
mx = min (mx, cnt);
if (mx == INF) mx = -1;
cout << mx;
return;
}
int main () {
Zymraq();
Solution ();
exit (0);
}
Compilation message
joi2019_ho_t3.cpp: In function 'void Solution()':
joi2019_ho_t3.cpp:66:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i = 0; i < s.size(); ++ i) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < s.size(); ++ i) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < odd.size(); ++ i) {
| ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:89:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | if (i + 1 == odd.size()) {
| ~~~~~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:94:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int j = i + 1; j < odd.size(); ++ j) {
| ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:106:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int i = 0; i < even.size(); ++ i) {
| ~~^~~~~~~~~~~~~
joi2019_ho_t3.cpp:108:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | if (i + 1 == even.size()) {
| ~~~~~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:113:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int j = i + 1; j < even.size(); ++ j) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |