#include<bits/stdc++.h>
using namespace std;
#define int long long
#define nl '\n'
inline void solve(){
int n;
cin>>n;
string s;
cin>>s;
vector<int> a(n);
for(int i=0; i<n; i++){
if(s[i] == 'R') a[i] = 0;
else if(s[i] == 'G') a[i] = 1;
else a[i] = 2;
}
auto b = a;
sort(b.begin(), b.end());
int ans = 1e18;
do{
auto c = b;
bool tf = true;
for(int i=0; i<n-1; i++){
if(c[i] == c[i+1]){
tf = false;
break;
}
}
if(!tf) continue;
int subans = 0;
for(int i=0; i<n; i++){
if(c[i] == a[i]) continue;
tf = false;
for(int j=i+1; j<n; j++){
if(c[j] != a[i]) continue;
for(int k=j; k>i; k--){
swap(c[k], c[k-1]);
subans++;
}
tf = true;
break;
}
if(!tf){
subans = 1e18;
break;
}
}
ans = min(ans, subans);
}while(next_permutation(b.begin(), b.end()));
cout<<(ans == 1e18 ? -1 : ans);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
8 ms |
336 KB |
Output is correct |
6 |
Correct |
14 ms |
336 KB |
Output is correct |
7 |
Correct |
11 ms |
336 KB |
Output is correct |
8 |
Correct |
15 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
9 ms |
336 KB |
Output is correct |
12 |
Correct |
17 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
336 KB |
Output is correct |
14 |
Correct |
5 ms |
336 KB |
Output is correct |
15 |
Correct |
11 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
8 ms |
336 KB |
Output is correct |
6 |
Correct |
14 ms |
336 KB |
Output is correct |
7 |
Correct |
11 ms |
336 KB |
Output is correct |
8 |
Correct |
15 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
9 ms |
336 KB |
Output is correct |
12 |
Correct |
17 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
336 KB |
Output is correct |
14 |
Correct |
5 ms |
336 KB |
Output is correct |
15 |
Correct |
11 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Execution timed out |
1042 ms |
336 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
8 ms |
336 KB |
Output is correct |
6 |
Correct |
14 ms |
336 KB |
Output is correct |
7 |
Correct |
11 ms |
336 KB |
Output is correct |
8 |
Correct |
15 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
9 ms |
336 KB |
Output is correct |
12 |
Correct |
17 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
336 KB |
Output is correct |
14 |
Correct |
5 ms |
336 KB |
Output is correct |
15 |
Correct |
11 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Execution timed out |
1042 ms |
336 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |