#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int maxn = 3000 + 5;
const int INF = 1e9;
const double eps = 1e-7;
struct info{
int s, v;
info(){}
info(int _s, int _v) : s(_s), v(_v){}
bool operator < (const info& other) const{
return v < other.v || (v == other.v && s < other.s);
}
};
int main(void){
fastio;
int n;
cin>>n;
string s;
cin>>s;
vector<int> pa, pb;
for(int i = 0; i < n; i++){
if(s[i] == 'R') pa.pb(i);
else pb.pb(i);
}
if(n % 2){
if(abs(int(pa.size() - pb.size())) > 1){
cout<<-1<<"\n";
return 0;
}
int x = 0, y = 0, ans = 0;
for(int i = 0; i < n; i++){
if(i % 2 == 0){
if(pa.size() > pb.size()){
ans += abs(i - pa[x]);
x++;
}
else {
ans += abs(i - pb[y]);
y++;
}
}
else{
if(pa.size() < pb.size()){
ans += abs(i - pa[x]);
x++;
}
else {
ans += abs(i - pb[y]);
y++;
}
}
}
cout<<ans / 2<<"\n";
}
else{
if(pa.size() != pb.size()){
cout<<-1<<"\n";
return 0;
}
vector<int> ans(2);
for(int i = 0; i < n; i++){
ans[i % 2] += abs(pa[i / 2] - i);
ans[1 - i % 2] += abs(pb[i / 2] - i);
}
cout<<min(ans[0], ans[1]) / 2<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |