This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,m,q;
vector<int> pw;
vector<int> b,w;
int getSum(int l, int r) {
if(l <= r) {
return pw[r] - ((l > 0) ? pw[l - 1] : 0);
}
return pw[r] + (pw[2*n-1] - ((l > 0) ? pw[l - 1] : 0));
}
int dif(int l, int r) {
if(l <= r) return r - l + 1;
else return 2*n + r - l + 1;
}
int calc_k(int k) {
int ret = 0;
for(int i = 0; i<n; i++) {
int j = (i + k) % n;
int wu = getSum(w[i], b[j]) - 1;
int wd = n - 1 - wu, bu = dif(w[i], b[j]) - 2 - wu;
int bd = n - 1 - bu;
// cout << " wi:" << w[i] << " bj:" << b[j] << " wu:" << wu << " wd:" << wd << " bu:" << bu << " bd:" << bd << "\n";
ret += min(wu, bd) + min(wd, bu);
}
// cout << ret << "\n";
return ret / 2;
}
inline void solve() {
cin >> n;
string s;
cin >> s;
pw.assign(2*n + 3, 0);
pw[0] = s[0] == 'W';
for(int i = 0; i<2*n; i++) {
if(s[i] == 'B') b.pb(i);
else w.pb(i);
}
for(int i = 1; i<2*n; i++) {
pw[i] = pw[i - 1] + (s[i] == 'W');
}
int ans = 0;
for(int k = 0; k < n; k++) {
ans = max(ans, calc_k(k));
}
cout << ans << "\n";
}
signed main() {
fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# | 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... |