#include<bits/stdc++.h>
#define st first
#define nd second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define pp pop_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){cerr<<h;if(sizeof...(t))cerr<<", ";debug(t...);};
#define deb(x...) cerr<<#x<<" = ";debug(x);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<19, INF=1e9+5;
int tab[N+N];
void add(int v, int c){
for(v+=N; v; v>>=1)tab[v]+=c;
}
int sum(int l, int r){
int ans=0;
for(l+=N,r+=N; l<r; l>>=1, r>>=1){
if(l&1)ans+=tab[l++];
if(r&1)ans+=tab[--r];
}
return ans;
}
ll solve(int n, vi A, vi B, int a){
int wsk=n, wsk2=n;
vector<pii> V(n);
for(int i=0; i<a; i++){
//deb(wsk, wsk2);
V[i]=mp(A[i], B[n-a+i]);
}
for(int i=a; i<n; i++){
V[i]=mp(A[i], B[i-a]);
}
for(pii &i:V)if(i.st>i.nd)swap(i.st, i.nd);
sort(all(V));
int ans=0;
for(int i=0; i<n; i++){
//deb(V[i]);
//deb(V[i].st, V[i].nd);
ans+=sum(V[i].st, V[i].nd);
add(V[i].nd, 1);
}
for(int i=0; i<n; i++){
add(V[i].nd, -1);
}
//deb(ans);
return ans;
}
int main(){
//ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
vi A, B;
for(int i=0; i<n+n; i++){
if(s[i]=='B')A.pb(i);
else B.pb(i);
}
ll ans=0;
int l=0, r=n-1;
while(l<r){
int d=(r-l+2)/3;
//deb(l, r);
//deb(l+d, r-d);
ll a1=solve(n, A, B, l+d), a2=solve(n, A, B, r-d);
ans=max(ans, a1);
ans=max(ans, a2);
if(a1>a2)r=r-d-1;
else l=l+d+1;
}
if(l==r)ans=max(ans, solve(n, A, B, l));
cout<<ans;
}
Compilation message
monochrome.cpp: In function 'll solve(int, vi, vi, int)':
monochrome.cpp:38:6: warning: unused variable 'wsk' [-Wunused-variable]
38 | int wsk=n, wsk2=n;
| ^~~
monochrome.cpp:38:13: warning: unused variable 'wsk2' [-Wunused-variable]
38 | int wsk=n, wsk2=n;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |