Submission #679045

# Submission time Handle Problem Language Result Execution time Memory
679045 2023-01-07T10:05:05 Z bashkort Monochrome Points (JOI20_monochrome) C++17
100 / 100
577 ms 5076 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, hi = R;
    calc(L), calc(R);
    while (lo < hi) {
        int mid = (lo + hi) >> 1;
        if (calc(mid + 1) >= calc(mid)) lo = mid + 1;
        else hi = mid;
    }


    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;
      |              ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 3 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 2 ms 356 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 ms 280 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 3 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 4 ms 340 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 3 ms 340 KB Output is correct
37 Correct 3 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 2 ms 356 KB Output is correct
42 Correct 3 ms 340 KB Output is correct
43 Correct 2 ms 340 KB Output is correct
44 Correct 3 ms 340 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 2 ms 340 KB Output is correct
47 Correct 2 ms 340 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 2 ms 280 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 3 ms 364 KB Output is correct
53 Correct 577 ms 4736 KB Output is correct
54 Correct 565 ms 4988 KB Output is correct
55 Correct 524 ms 4828 KB Output is correct
56 Correct 531 ms 4988 KB Output is correct
57 Correct 418 ms 4852 KB Output is correct
58 Correct 443 ms 5052 KB Output is correct
59 Correct 415 ms 4732 KB Output is correct
60 Correct 437 ms 4752 KB Output is correct
61 Correct 432 ms 5048 KB Output is correct
62 Correct 446 ms 4920 KB Output is correct
63 Correct 477 ms 4904 KB Output is correct
64 Correct 410 ms 4776 KB Output is correct
65 Correct 430 ms 4680 KB Output is correct
66 Correct 359 ms 4812 KB Output is correct
67 Correct 360 ms 4820 KB Output is correct
68 Correct 340 ms 4520 KB Output is correct
69 Correct 441 ms 4964 KB Output is correct
70 Correct 368 ms 4576 KB Output is correct
71 Correct 61 ms 5056 KB Output is correct
72 Correct 60 ms 5076 KB Output is correct
73 Correct 476 ms 4948 KB Output is correct