Submission #306846

#TimeUsernameProblemLanguageResultExecution timeMemory
306846MrDominoMonochrome Points (JOI20_monochrome)C++14
100 / 100
48 ms3084 KiB
#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;

ll get(int k)
{
    ll sol = 0;
    for (int i = 0; i < n; i++)
    {
        int l = a[i];
        int r = b[(i + k) % n];
        if (l > r)
        {
            swap(l, r);
        }
        int x = r - l - 1;
        int y = 2 * n - x - 2;
        sol += min(x, y) ;
    }
    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;
        }
    }
    sol /= 2;
    cout << sol << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...