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;
typedef long long ll;
int n;
string s;
vector<int> b, w;
ll Calc(int k){
ll res = 0;
int j = k;
for(int i = 0; i < n; i++){
int dist = abs(b[i] - w[j]);
res += min(dist, 2 * n - dist);
j = (j == n - 1? 0 : j + 1);
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for(int i = 0; i < 2 * n; i++)
if (s[i] == 'B')
b.push_back(i);
else
w.push_back(i);
ll a = Calc(0);
ll b = Calc(n - 1);
ll answer = max(a, b);
int l = 1, r = n - 2;
if (a > b){
while (l <= r){
int mid = (l + r) >> 1;
ll val = Calc(mid);
if (val <= b){
r = mid - 1;
continue;
}
ll val2 = Calc(mid + 1);
if (val < val2){
answer = max(answer, val2);
l = mid + 2;
} else {
answer = max(answer, val);
r = mid - 1;
}
}
} else {
while (l <= r){
int mid = (l + r) >> 1;
ll val = Calc(mid);
if (val <= a){
l = mid + 1;
continue;
}
ll val2 = Calc(mid + 1);
if (val < val2){
answer = max(answer, val2);
l = mid + 2;
} else {
answer = max(answer, val);
r = mid - 1;
}
}
}
cout << (answer - n) / 2;
return 0;
}
# | 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... |