#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct BIT{
ll n;
vector<ll> a;
BIT(ll N):n(N),a(N+1){}
void add(ll i, ll x){
for(;i<=n;i+=i&-i) a[i] += x;
}
ll get(ll i){
ll x = 0;
for(;i;i-=i&-i) x +=a[i];
return x;
}
};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n;
string s;
cin >> n >> s;
vector<ll> cnt(3,0);
vector<ll> a(n);
for(ll i = 0;i<n;i++) {
a[i] = s[i]=='R'?0:s[i]=='G'?1:2;
cnt[a[i]]++;
}
vector<ll> b(n);
iota(b.begin(),b.end(),1);
ll ans = 0;
for(ll i = 1;i<n-1;i++){
if(s[i]==s[i-1]){
ll j = i+1;
while(s[j]==s[i]&&j<n-1) j++;
for(ll k = j;k>i;k--) swap(s[k],s[k-1]);
for(ll k = j;k>i;k--) swap(b[k],b[k-1]);
}
}
for(ll i = n-2;i>0;i--){
if(s[i]==s[i+1]){
ll j = i-1;
while(s[j]==s[i]&&j>0) j--;
for(ll k = j;k<i;k++) swap(s[k],s[k+1]);
for(ll k = j;k<i;k++) swap(b[k],b[k+1]);
}
}
for(ll i = 1;i<n;i++){
if(s[i]==s[i-1]){
cout << "-1\n";
return 0;
}
}
BIT t(n);
for(ll i = n-1;i>=0;i--){
ans += t.get(b[i]);
t.add(b[i],1);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |