답안 #679043

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679043 2023-01-07T10:02:05 Z bashkort Monochrome Points (JOI20_monochrome) C++17
35 / 100
2000 ms 4184 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);
    for (int w = L; w <= R; ++w) {
        if (calc(w) == -1) {
            cerr << w << " ";
        }
    }
    cerr << endl;


    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 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 5 ms 212 KB Output is correct
15 Correct 4 ms 212 KB Output is correct
16 Correct 4 ms 212 KB Output is correct
17 Correct 5 ms 328 KB Output is correct
18 Correct 2 ms 212 KB Output is correct
19 Correct 2 ms 212 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
21 Correct 3 ms 316 KB Output is correct
22 Correct 4 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 212 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 320 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 5 ms 212 KB Output is correct
15 Correct 4 ms 212 KB Output is correct
16 Correct 4 ms 212 KB Output is correct
17 Correct 5 ms 328 KB Output is correct
18 Correct 2 ms 212 KB Output is correct
19 Correct 2 ms 212 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
21 Correct 3 ms 316 KB Output is correct
22 Correct 4 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 212 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 320 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 196 ms 340 KB Output is correct
35 Correct 199 ms 340 KB Output is correct
36 Correct 189 ms 352 KB Output is correct
37 Correct 178 ms 448 KB Output is correct
38 Correct 86 ms 344 KB Output is correct
39 Correct 94 ms 348 KB Output is correct
40 Correct 124 ms 340 KB Output is correct
41 Correct 111 ms 340 KB Output is correct
42 Correct 187 ms 352 KB Output is correct
43 Correct 72 ms 352 KB Output is correct
44 Correct 151 ms 340 KB Output is correct
45 Correct 14 ms 364 KB Output is correct
46 Correct 13 ms 340 KB Output is correct
47 Correct 17 ms 356 KB Output is correct
48 Correct 76 ms 340 KB Output is correct
49 Correct 56 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 162 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 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 5 ms 212 KB Output is correct
15 Correct 4 ms 212 KB Output is correct
16 Correct 4 ms 212 KB Output is correct
17 Correct 5 ms 328 KB Output is correct
18 Correct 2 ms 212 KB Output is correct
19 Correct 2 ms 212 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
21 Correct 3 ms 316 KB Output is correct
22 Correct 4 ms 212 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 212 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 316 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 320 KB Output is correct
33 Correct 4 ms 340 KB Output is correct
34 Correct 196 ms 340 KB Output is correct
35 Correct 199 ms 340 KB Output is correct
36 Correct 189 ms 352 KB Output is correct
37 Correct 178 ms 448 KB Output is correct
38 Correct 86 ms 344 KB Output is correct
39 Correct 94 ms 348 KB Output is correct
40 Correct 124 ms 340 KB Output is correct
41 Correct 111 ms 340 KB Output is correct
42 Correct 187 ms 352 KB Output is correct
43 Correct 72 ms 352 KB Output is correct
44 Correct 151 ms 340 KB Output is correct
45 Correct 14 ms 364 KB Output is correct
46 Correct 13 ms 340 KB Output is correct
47 Correct 17 ms 356 KB Output is correct
48 Correct 76 ms 340 KB Output is correct
49 Correct 56 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 162 ms 340 KB Output is correct
53 Execution timed out 2056 ms 4184 KB Time limit exceeded
54 Halted 0 ms 0 KB -