#include<bits/stdc++.h>
// #include<atcoder/modint>
#define pb push_back
#define mp make_pair
#define rs resize
#define ft front()
#define bk back()
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define r0f(i,n) for(int i = n-1; i >= 0; i--)
#define R0F(i,k,n) for(int i = n-1; i >= k; i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define pout(a) cout<<a.first<<' '<<a.second<<'\n'
#define vout(v) for(auto u : v){cout<<u<<' ';} cout<<endl
#define dout(a) cout<<a<<' '<<#a<<endl
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<endl
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
#define p2(x) (1LL << (x))
#define pct(x) (__builtin_popcountll(x))
#define ist(x,y) ((x >> y) & 1)
template<class T, class U> bool ckmin(T& a, const U& b) { return (T)b < a ? a = (T)b, 1 : 0; }
template<class T, class U> bool ckmax(T& a, const U& b) { return a < (T)b ? a = (T)b, 1 : 0; }
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
// using namespace atcoder;
// using mint = modint998244353;
bool cmp(pii a, pii b){
if(a.first != b.first)return a.first < b.first;
else return a.second > b.second;
}
struct segtree{
int n; vi tree;
segtree(int x){
n = x; tree.rs(2 * n + 5);
}
void add(int k, int x){
k += n; tree[k] += x;
for(k/=2;k>=1;k/=2)tree[k] = tree[k*2] + tree[k*2+1];
}
int quer(int l, int r){
l += n; r += n; int ret = 0;
while(l <= r){
if(l % 2)ret += tree[l++];
if(r % 2 == 0)ret += tree[r--];
l/=2;r/=2;
}
// dout2(l,r); dout(ret);
return ret;
}
};
const int mxn = 2e5 + 5;
int n, mem[mxn]; vi a,b;
int solve(vpii v){
sort(all(v), cmp);
// for(auto u : v)pout(u); cout<<'\n';
segtree S = segtree(2*n); int nst = 0, ans = -n;
f0r(i,n){
nst += S.quer(v[i].second + 1, 2*n-1);
S.add(v[i].second, 1); ans += v[i].second - v[i].first;
}
ans -= nst * 2; ans /= 2;
//dout(ans);
return ans;
}
int f(int x){
if(mem[x] != -4e18)return mem[x];
vpii tmp; f0r(j, x)tmp.pb(mp(a[j], b[j + n - x])); f0r(j, n-x)tmp.pb(mp(b[j], a[j+x]));
int d = solve(tmp); mem[x] = d; return d;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
cin>>n; f0r(i, n + 5)mem[i] = -4e18; string s; cin>>s; f0r(i, 2*n){if(s[i] == 'W')a.pb(i); else b.pb(i);}
int ans = 0;
//diff point i is between i and i+1, need to find the max point from f(0) to f(n)
int lo = 0, hi = n; //binary search to find the first point such that the diff is -
while(lo < hi){
int mid = lo + hi >> 1;
if(f(mid+1) - f(mid) < 0){
hi = mid;
}
else lo = mid + 1;
}
out(f(lo));
/*
f0r(i, n+1){ //pair first i whites with last i blacks, pair first n-i blacks with last n-i whites
vpii tmp; f0r(j, i)tmp.pb(mp(a[j], b[j + n - i])); f0r(j, n-i)tmp.pb(mp(b[j], a[j+i]));
ckmax(ans, solve(tmp));
}
out(ans);
*/
}
# | 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... |