This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll INF = 1e18;
struct Seg{
int tree[800800], sz;
void init(int n){
sz = n;
fill(tree, tree+sz*2, 0);
}
void update(int p, int x){
for (tree[p+=sz]+=x;p>1;p>>=1) tree[p>>1] = tree[p] + tree[p^1];
}
int query(int l, int r){
int ret = 0;
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret += tree[l++];
if (r&1) ret += tree[--r];
}
return ret;
}
}tree;
char a[400400];
int B[200200], W[200200], arr[400400];
vector<pair<int, int>> E;
ll calc(int n){
for (auto &p:E){
if (p.first > p.second) swap(p.first, p.second);
arr[p.first] = -1;
arr[p.second] = p.first;
}
tree.init(n+1);
ll ret = 0;
for (int i=1;i<=n;i++){
if (arr[i]==-1) tree.update(i, 1);
else{
tree.update(arr[i], -1);
ret += tree.query(arr[i]+1, i);
}
}
return ret;
}
ll calc2(int n, int z){
E.clear();
int pt = z;
for (int i=1;i<=n/2;i++){
E.emplace_back(B[i], W[pt]);
pt++;
if (pt>n/2) pt -= n/2;
}
return calc(n);
}
int main(){
int n;
scanf("%d", &n);
scanf("%s", a+1);
n *= 2;
int cb = 0, cw = 0;
for (int i=1;i<=n;i++){
if (a[i]=='B') B[++cb] = i;
else W[++cw] = i;
}
ll ans = 0;
if (n<=2000){
for (int z=1;z<=n/2;z++){
ans = max(ans, calc2(n, z));
}
}
else{
int l = 1, r = n/2;
while(r-l>=10){
int m1 = (l*2+r)/3, m2 = (l+r*2)/3;
if (calc2(n, m1)<calc2(n, m2)) l = m1+1;
else r = m1-1;
}
for (int z=l;z<=r;z++) ans = max(ans, calc2(n, z));
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
monochrome.cpp: In function 'int main()':
monochrome.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
monochrome.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%s", a+1);
| ~~~~~^~~~~~~~~~~
# | 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... |