#include <bits/stdc++.h>
//qwerty47924692
using namespace std;
using ll = long long;
#define f first
#define s second
const ll N=3e5+29;
ll cur1,cur2,cnt[3],n,ans=1e18;
vector<ll>pos[3];
string s;
void f(ll x,ll cntr,ll cnty,ll cntg,ll sum,char last){
if(x==n-1){
ans=min(ans,sum);
return;
}
x++;
if(last!='R'&&cntr+1<=cnt[0]){
f(x,cntr+1,cnty,cntg,sum+abs(x-pos[0][cntr]),'R');
}if(last!='Y'&&cnty+1<=cnt[1]){
f(x,cntr,cnty+1,cntg,sum+abs(x-pos[1][cnty]),'Y');
}if(last!='G'&&cntg+1<=cnt[2]){
f(x,cntr,cnty,cntg+1,sum+abs(x-pos[2][cntg]),'G');
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>s;
ll x=0;
if(n>15)return 0;
for(char i:s){
if(i=='R'){
cnt[0]++;
pos[0].push_back(x++);
}
if(i=='Y'){
cnt[1]++;
pos[1].push_back(x++);
}
if(i=='G'){
cnt[2]++;
pos[2].push_back(x++);
}
}
f(-1,0,0,0,0,'J');
cout<<(ans==1e18 ? -1 : ans/2);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |