이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #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;
}
int ceil_div(int x, int y) {
return (x - 1) / y + 1;
}
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 tl = 0, tr = n - 1;
while(tl < tr) {
int tm1 = tl + (tr - tl) / 3;
int tm2 = tl + ceil_div(2*(tr - tl), 3);
if(calc_k(tm1) < calc_k(tm2)) {
tl = tm1 + 1;
}
else {
tr = tm2 - 1;
}
}
cout << calc_k(tl) << "\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... |