#include <bits/stdc++.h>
#define uwu return 0;
using namespace std;
#define fs first
#define sc second
const int SIZE = 2e5 + 5, TL = 1900;
string S;
int pos[2][SIZE], seg[SIZE];
int BITree[SIZE];
void modify(int pos, int val){
for (; pos < SIZE; pos += (pos & - pos)){
BITree[pos] += val;
}
return;
}
int query(int pos){
int ret = 0;
for (; pos; pos -= (pos & - pos)){
ret += BITree[pos];
}
return ret;
}
#define is_W(i) (S[(i + x) % (2 * N)] == 'W')
long long eval(int x, int N){
for (int i = 0; i < 2 * N; i++){
BITree[i] = 0;
}
int ptr[2] = {0, 0};
for (int i = N; i < S.size(); i++){
pos[is_W(i)][ptr[is_W(i)]++] = i;
}
ptr[0] = 0, ptr[1] = 0;
long long ans = 0;
for (int i = 0; i < N; i++){
ans += query(pos[1 - is_W(i)][ptr[1 - is_W(i)]]);
modify(pos[1 - is_W(i)][ptr[1 - is_W(i)]++], 1);
}
return ans;
}
mt19937 rng(time(0));
int main(){
cin.tie(0), ios::sync_with_stdio(0);
int N;
cin >> N >> S;
long long ans = 0;
auto ck = clock();
for (; int(clock() - ck) < TL;){
ans = max(ans, eval(rng() % (2 * N), N));
}
cout << ans << '\n';
uwu
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |