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>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 2e9+5
#define eps 1e-7
#define eps1 1e-25
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
#define V 450
using namespace std;
const int MOD = 1e9;
const int nax = 2e5+5;
const int lg = 20;
ll dp[nax];
int n;
vi blck, wht;
ll test(int md){
if(dp[md] != -1) return dp[md];
ll res = 0;
for (int j = 0; j < n; ++j)
{
int ind = (j+md)%n;
int a = blck[j], b = wht[ind];
if(a > b) swap(a,b);
int bt = b-a-1;
res += min(bt, 2*n-bt-2);
}
res /= 2;
return dp[md] = res;
}
int main() {
optimise;
cin >> n;
string s;
cin >> s;
memset(dp, -1, sizeof dp);
for (int i = 0; i < n*2; ++i)
{
if(s[i] == 'B') blck.pb(i);
if(s[i] == 'W') wht.pb(i);
}
ll mx = 0;
int l = 0, r = n;
while (l < r)
{
int md = (l+r)/2;
ll a = test(md+1), b = test(md);
if(a > b){
mx = max(mx, a);
l = md+1;
} else {
mx = max(mx, b);
r = md;
}
}
cout << mx << '\n';
}
# | 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... |