답안 #679044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679044 2023-01-07T10:03:48 Z bashkort Monochrome Points (JOI20_monochrome) C++17
0 / 100
0 ms 212 KB
#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;

    vector<int> p(2 * n);

    auto check_w = [&](int w) -> bool {
        int b = n - w;
        int left[2]{w, b}, pos[2]{};
        bool yay = true;
        for (int i = 0; i < 2 * n; ++i) {
            int tp = s[i] == 'B';
            if (left[tp]) {
                pos[tp] += 1;
                --left[tp];
            } else {
                if (pos[tp ^ 1] == 0 && tp == 1) {
                    return false;
                }
                pos[tp ^ 1] -= 1;
            }
        }
        return true;
    };


    auto check_b = [&](int b) -> bool {
        int w = n - b;
        int left[2]{w, b}, pos[2]{};
        bool yay = true;
        for (int i = 0; i < 2 * n; ++i) {
            int tp = s[i] == 'B';
            if (left[tp]) {
                pos[tp] += 1;
                --left[tp];
            } else {
                if (pos[tp ^ 1] == 0 && tp == 0) {
                    return false;
                }
                pos[tp ^ 1] -= 1;
            }
        }
        return true;
    };

    ll ans = 0;
    auto calc = [&](int w) -> ll {
        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) {
            ll now = intersecting(p);
            ans = max(ans, now);
            return now;
        } else {
            return -1;
        }
    };

    int lo = 0, hi = n + 1;
    while (lo + 1 < hi) {
        int mid = (lo + hi) >> 1;
        if (check_w(mid)) lo = mid;
        else hi = mid;
    }
    int R = lo;

    lo = 0, hi = n + 1;
    while (lo + 1 < hi) {
        int mid = (lo + hi) >> 1;
        if (check_b(mid)) lo = mid;
        else hi = mid;
    }
    int L = n - lo;
    assert(L <= R);

    lo = L - 1, hi = R;
    while (lo + 1 < hi) {
        int mid = (lo + hi) >> 1;
        if (calc(mid) >= calc(mid + 1)) lo = mid;
        else hi = mid;
    }
    calc(lo);
    calc(hi);
    calc(lo + 1);
    calc(hi - 1);


    cout << ans << '\n';


    return 0;
}

Compilation message

monochrome.cpp: In constructor 'Fenwick::Fenwick(int)':
monochrome.cpp:8:9: warning: 'Fenwick::n' will be initialized after [-Wreorder]
    8 |     int n{};
      |         ^
monochrome.cpp:7:17: warning:   'std::vector<int> Fenwick::a' [-Wreorder]
    7 |     vector<int> a;
      |                 ^
monochrome.cpp:12:5: warning:   when initialized here [-Wreorder]
   12 |     Fenwick(int n) : n(n), a(n + 1) {}
      |     ^~~~~~~
monochrome.cpp: In lambda function:
monochrome.cpp:59:14: warning: unused variable 'yay' [-Wunused-variable]
   59 |         bool yay = true;
      |              ^~~
monochrome.cpp: In lambda function:
monochrome.cpp:79:14: warning: unused variable 'yay' [-Wunused-variable]
   79 |         bool yay = true;
      |              ^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -