#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4e5 + 6;
string s; int n,bi = 0, wi = 0, b[N],w[N],BIT[N];
void update(int id)
{
for(;id <= 2*n; id += (-id)&id) BIT[id] += 1;
}
int sum(int id)
{
int res = 0;
for(;id > 0; id -= (-id)&id) res += BIT[id];
return res;
}
int get(int l, int r)
{
return (sum(r) - sum(l-1));
}
int cal(int k)
{
vector<pair<int,int>> edge;
memset(BIT,0,sizeof(BIT));
for(int i = 0; i < n; i++)
{
int c = min(w[i],b[(i+k)%n]) , d = max(w[i],b[(i+k)%n]);
edge.push_back({c,d});
}
sort(edge.begin(),edge.end());
int res = 0;
for(auto [l,r] : edge)
{
res += get(l+1,r+1);
update(r+1);
}
return res;
}
double max_f(double left, double right) {
double N_ITER = 500;
for (double i = 0; i < N_ITER; i++) {
double x1 = left + (right - left) / 3.0;
double x2 = right - (right - left) / 3.0;
if (cal(x1) > cal(x2)) right = x2;
else left = x1;
}
return cal(left);
}
main()
{
cin >> n >> s;
for(int i = 0; i < 2*n; i++)
{
if(s[i] == 'W') w[wi++] = i;
else b[bi++] = i;
}
int left = 0, right = n - 1, mid, ans = 0;
while(left <= right){
mid = (right - left) / 2 + left;
int curr = cal( mid);
int prev = 0;
if (mid - 1 >= 0){
prev = cal( mid - 1);
}
ans = max(ans, curr);
if (curr > prev){
left = mid + 1;
}
else{
right = mid - 1;
}
}
ans = max(ans, cal( 0));
ans = max(ans, cal( n - 1));
cout << ans << "\n";
}
Compilation message (stderr)
monochrome.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
51 | main()
| ^~~~
# | 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... |