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;
typedef long double ld;
const int N = (int) 2e5 + 7;
int n;
string s;
vector<int> a;
vector<int> b;
int t[2 * N];
void clr()
{
for (int i = 1; i <= 2 * n; i++)
{
t[i] = 0;
}
}
void add(int i, int x)
{
i++;
while (i <= 2 * n)
{
t[i] += x;
i += i & (-i);
}
}
int sum(int i)
{
i++;
int sol = 0;
while (i)
{
sol += t[i];
i -= i & (-i);
}
return sol;
}
int sum(int i, int j)
{
return sum(j) - sum(i - 1);
}
ll get(int k)
{
vector<pair<int, int>> p;
for (int i = 0; i < n; i++)
{
p.push_back({a[i], b[(i + k) % n]});
}
for (auto &it : p)
{
if (it.second < it.first)
{
swap(it.first, it.second);
}
}
sort(p.begin(), p.end());
ll sol = 0;
clr();
for (int i = 0; i < (int) p.size(); i++)
{
sol += sum(p[i].first, p[i].second);
add(p[i].second, +1);
}
return sol;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (int i = 0; i < 2 * n; i++)
{
if (s[i] == 'W')
{
a.push_back(i);
}
else
{
b.push_back(i);
}
}
int l = 0, r = n - 2;
ll sol = max(get(0), get(n - 1));
while (l <= r)
{
int m = (l + r) / 2;
ll x = get(m);
ll y = get(m + 1);
sol = max(sol, max(x, y));
if (x < y)
{
l = m + 1;
}
else
{
r = m - 1;
}
}
cout << sol << "\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... |