# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
516577 | leinad2 | Monochrome Points (JOI20_monochrome) | C++17 | 1512 ms | 8056 KiB |
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>
using namespace std;
int n, i, j, k, a, b;
long long ans=0;
char S[400010];
vector<int>A, B;
int tree[400010];
void init(){for(int i=1;i<=2*n;i++)tree[i]=0;}
void update(int pos, int v){while(pos<=2*n)tree[pos]+=v,pos+=(pos&-pos);}
int get(int pos){int res=0;while(pos)res+=tree[pos],pos-=(pos&-pos);return res;}
long long f(int i)
{
init();
vector<pair<int, int> >V;
for(int j=i;j<n;j++)V.push_back({A[j-i], B[j]});
for(int j=0;j<i;j++)V.push_back({A[j+n-i], B[j]});
for(auto &p:V)if(p.first>p.second)swap(p.first, p.second);
sort(V.begin(), V.end());
long long res=0;
for(auto p:V)
{
int a=p.second;
update(a, 1);
res+=get(a-1)-get(p.first);
}
ans=max(ans, res);
return res;
}
main()
{
scanf("%d", &n);
scanf("%s", &S);
for(i=0;i<2*n;i++)
{
if(S[i]=='B')A.push_back(i+1);
else B.push_back(i+1);
}
int a=0, b=n-1;
while(a+3<=b)
{
int m1=(a+a+b)/3, m2=(a+b+b)/3;
if(f(m1)>f(m2))b=m2;
else a=m1;
}
for(i=a;i<=b;i++)f(i);
cout<<ans;
}
Compilation message (stderr)
# | 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... |