| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 679040 | bashkort | Monochrome Points (JOI20_monochrome) | C++17 | 2085 ms | 4324 KiB | 
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;
using ll = long long;
struct Fenwick {
    vector<int> a;
    int n{};
    Fenwick() = default;
    Fenwick(int n) : n(n), a(n + 1) {}
    int sum(int i) {
        int ans = 0;
        for (int x = i + 1; x > 0; x -= x & -x) {
            ans += a[x];
        }
        return ans;
    }
    void modify(int i, int val) {
        for (int x = i + 1; x <= n; x += x & -x) {
            a[x] += val;
        }
    }
};
ll intersecting(vector<int> &p) {
    int n = p.size();
    Fenwick fn(n);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        if (p[i] > i) {
            ans -= fn.sum(i);
            continue;
        }
        ans += fn.sum(p[i]);
        fn.modify(p[i], 1);
    }
    return ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    string s;
    cin >> s;
    ll ans = 0;
    vector<int> p(2 * n);
    for (int w = 0; w <= n; ++w) {
        int b = n - w;
        queue<int> pos[2];
        int left[2]{w, b};
        bool yay = true;
        for (int i = 0; i < 2 * n; ++i) {
            int tp = s[i] == 'B';
            if (left[tp]) {
                pos[tp].push(i);
                --left[tp];
            } else {
                if (pos[tp ^ 1].empty()) {
                    yay = false;
                    break;
                }
                p[i] = pos[tp ^ 1].front();
                p[p[i]] = i;
                pos[tp ^ 1].pop();
            }
        }
        if (yay) {
            ans = max(ans, intersecting(p));
        }
    }
    cout << ans << '\n';
    return 0;
}
Compilation message (stderr)
| # | 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... | ||||
