Submission #679040

# Submission time Handle Problem Language Result Execution time Memory
679040 2023-01-07T09:35:43 Z bashkort Monochrome Points (JOI20_monochrome) C++17
35 / 100
2000 ms 4324 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;

    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

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) {}
      |     ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 340 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 340 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 328 KB Output is correct
15 Correct 4 ms 324 KB Output is correct
16 Correct 4 ms 212 KB Output is correct
17 Correct 5 ms 212 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 212 KB Output is correct
22 Correct 3 ms 316 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 316 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 1 ms 212 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 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 4 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 340 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 328 KB Output is correct
15 Correct 4 ms 324 KB Output is correct
16 Correct 4 ms 212 KB Output is correct
17 Correct 5 ms 212 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 212 KB Output is correct
22 Correct 3 ms 316 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 316 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 1 ms 212 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 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 4 ms 212 KB Output is correct
34 Correct 198 ms 340 KB Output is correct
35 Correct 198 ms 348 KB Output is correct
36 Correct 192 ms 344 KB Output is correct
37 Correct 178 ms 340 KB Output is correct
38 Correct 89 ms 356 KB Output is correct
39 Correct 100 ms 324 KB Output is correct
40 Correct 126 ms 444 KB Output is correct
41 Correct 114 ms 356 KB Output is correct
42 Correct 187 ms 348 KB Output is correct
43 Correct 79 ms 404 KB Output is correct
44 Correct 154 ms 340 KB Output is correct
45 Correct 28 ms 340 KB Output is correct
46 Correct 29 ms 340 KB Output is correct
47 Correct 32 ms 340 KB Output is correct
48 Correct 87 ms 320 KB Output is correct
49 Correct 70 ms 320 KB Output is correct
50 Correct 17 ms 340 KB Output is correct
51 Correct 17 ms 340 KB Output is correct
52 Correct 164 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 320 KB Output is correct
9 Correct 1 ms 340 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 328 KB Output is correct
15 Correct 4 ms 324 KB Output is correct
16 Correct 4 ms 212 KB Output is correct
17 Correct 5 ms 212 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 212 KB Output is correct
22 Correct 3 ms 316 KB Output is correct
23 Correct 2 ms 212 KB Output is correct
24 Correct 2 ms 316 KB Output is correct
25 Correct 3 ms 212 KB Output is correct
26 Correct 1 ms 212 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 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 4 ms 212 KB Output is correct
34 Correct 198 ms 340 KB Output is correct
35 Correct 198 ms 348 KB Output is correct
36 Correct 192 ms 344 KB Output is correct
37 Correct 178 ms 340 KB Output is correct
38 Correct 89 ms 356 KB Output is correct
39 Correct 100 ms 324 KB Output is correct
40 Correct 126 ms 444 KB Output is correct
41 Correct 114 ms 356 KB Output is correct
42 Correct 187 ms 348 KB Output is correct
43 Correct 79 ms 404 KB Output is correct
44 Correct 154 ms 340 KB Output is correct
45 Correct 28 ms 340 KB Output is correct
46 Correct 29 ms 340 KB Output is correct
47 Correct 32 ms 340 KB Output is correct
48 Correct 87 ms 320 KB Output is correct
49 Correct 70 ms 320 KB Output is correct
50 Correct 17 ms 340 KB Output is correct
51 Correct 17 ms 340 KB Output is correct
52 Correct 164 ms 464 KB Output is correct
53 Execution timed out 2085 ms 4324 KB Time limit exceeded
54 Halted 0 ms 0 KB -