Submission #583494

# Submission time Handle Problem Language Result Execution time Memory
583494 2022-06-25T12:22:28 Z Cyanmond Boarding Passes (BOI22_passes) C++17
100 / 100
587 ms 377436 KB
#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

using i64 = long long int;
constexpr i64 inf64 = numeric_limits<i64>::max() / 3;

#define ALL(x) (x).begin(), (x).end()
#define REP_3(i, l, r) for (int i = (l); i < (r); ++i)
#define RVP_3(i, l, r) for (int i = (r - 1); i >= (l); --i)
#define REP(i, N) REP_3(i, 0, N)
#define RVP(i, N) RVP_3(i, 0, N)

void answer(const i64 x) {
    cout << x / 2;
    if (x % 2 == 1) cout << ".5" << endl;
}

template <typename T> void chmin(T &a, const T b) {
    if (a > b) a = b;
}

int main() {
    string s;
    cin >> s;
    vector<int> S;
    for (const int e : s) S.push_back(e - 'A');
    const int N = (int)S.size();
    const int G = *max_element(ALL(S)) + 1;

    vector<vector<int>> g_list(G);
    REP(i, N) g_list[S[i]].push_back(i);

    vector<vector<i64>> l_v(G, vector<i64>(N + 1));
    auto r_v = l_v;
    vector l_v_w(G, vector(G, vector(N + 1, (i64)0)));
    auto r_v_w = l_v_w;

    REP(i, N) {
        ++l_v[S[i]][i + 1];
        ++r_v[S[i]][i];
        REP(x, G) {
            const int p1 = (int)(upper_bound(ALL(g_list[x]), i) - g_list[x].begin()) - 1;
            const int p2 = (int)(lower_bound(ALL(g_list[x]), i) - g_list[x].begin());
            l_v_w[S[i]][x][i + 1] += (int)g_list[x].size() - p1;
            r_v_w[S[i]][x][i] += p2;
        }
    }
    REP(x, G) {
        REP(i, N) l_v[x][i + 1] += l_v[x][i];
        RVP(i, N) r_v[x][i] += r_v[x][i + 1];
        REP(y, G) {
            REP(i, N) l_v_w[x][y][i + 1] += l_v_w[x][y][i];
            RVP(i, N) r_v_w[x][y][i] += r_v_w[x][y][i + 1];
        }
    }

    auto calc_bound = [&](const int s, const int x) {
        i64 all_sum = 0;
        REP(i, G) {
            if (s & (1 << i)) all_sum += l_v[i][g_list[x].back()] * 2;
            if (i == x) all_sum += l_v[i][g_list[x].back()];
        }
        int ok = -1, ng = (int)g_list[x].size();
        while (ng - ok > 1) {
            const int m = (ok + ng) / 2;
            const int p = g_list[x][m];
            i64 l_sum = 0, r_sum = 0;
            REP(i, G) {
                const i64 l = l_v[i][p + 1], r = r_v[i][p];
                if (s & (1 << i)) {
                    l_sum += l * 2;
                    r_sum += r * 2;
                }
                if (i == x) {
                    l_sum += l;
                    r_sum += r;
                }
            }
            (l_sum <= r_sum ? ok : ng) = m;
        }
        return ok;
        // time complexity O(G log N)
    };

    auto calc_cost = [&](const int s, const int x, const int m) {
        i64 ret = 0;
        const int z = (int)g_list[x].size();
        if (m != -1) {
            const int p = g_list[x][m];
            REP(i, G) {
                if (s & (1 << i)) ret += (l_v_w[i][x][p] - l_v[i][p] * (z - m)) * 2;
                if (i == x) ret += l_v_w[i][x][p] - l_v[i][p] * (z - m);
            }
        }
        if (m != z - 1) {
            const int p = g_list[x][m + 1];
            REP(i, G) {
                if (s & (1 << i)) ret += (r_v_w[i][x][p + 1] - r_v[i][p + 1] * (m + 1)) * 2;
                if (i == x) ret += r_v_w[i][x][p + 1] - r_v[i][p + 1] * (m + 1);
            }
        }
        return ret;
    };

    vector<i64> dp(1 << G, inf64);
    dp[0] = 0;

    REP(bit, 1 << G) {
        REP(x, G) {
            if (g_list[x].empty()) {
                chmin(dp[bit | (1 << x)], dp[bit]);
                continue;
            }
            if (bit & (1 << x)) continue;
            const int m = calc_bound(bit, x);
            const i64 cost = calc_cost(bit, x, m);
            chmin(dp[bit | (1 << x)], dp[bit] + cost);
        }
    }
    answer(dp[(1 << G) - 1]);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 18 ms 4260 KB Output is correct
7 Correct 19 ms 4944 KB Output is correct
8 Correct 18 ms 5240 KB Output is correct
9 Correct 17 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 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 304 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 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 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 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 10 ms 11732 KB Output is correct
36 Correct 10 ms 11732 KB Output is correct
37 Correct 21 ms 17652 KB Output is correct
38 Correct 17 ms 17596 KB Output is correct
39 Correct 17 ms 17684 KB Output is correct
40 Correct 20 ms 17476 KB Output is correct
41 Correct 26 ms 17228 KB Output is correct
42 Correct 21 ms 17292 KB Output is correct
43 Correct 21 ms 17304 KB Output is correct
44 Correct 20 ms 17292 KB Output is correct
45 Correct 21 ms 17236 KB Output is correct
46 Correct 20 ms 17308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 18 ms 4260 KB Output is correct
7 Correct 19 ms 4944 KB Output is correct
8 Correct 18 ms 5240 KB Output is correct
9 Correct 17 ms 5204 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 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 0 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 304 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 10 ms 11732 KB Output is correct
45 Correct 10 ms 11732 KB Output is correct
46 Correct 21 ms 17652 KB Output is correct
47 Correct 17 ms 17596 KB Output is correct
48 Correct 17 ms 17684 KB Output is correct
49 Correct 20 ms 17476 KB Output is correct
50 Correct 26 ms 17228 KB Output is correct
51 Correct 21 ms 17292 KB Output is correct
52 Correct 21 ms 17304 KB Output is correct
53 Correct 20 ms 17292 KB Output is correct
54 Correct 21 ms 17236 KB Output is correct
55 Correct 20 ms 17308 KB Output is correct
56 Correct 15 ms 596 KB Output is correct
57 Correct 56 ms 596 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 15 ms 4216 KB Output is correct
64 Correct 17 ms 4908 KB Output is correct
65 Correct 18 ms 5192 KB Output is correct
66 Correct 17 ms 5196 KB Output is correct
67 Correct 0 ms 212 KB Output is correct
68 Correct 0 ms 212 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct
81 Correct 1 ms 212 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 10 ms 11732 KB Output is correct
85 Correct 10 ms 11664 KB Output is correct
86 Correct 20 ms 17656 KB Output is correct
87 Correct 16 ms 17620 KB Output is correct
88 Correct 15 ms 17620 KB Output is correct
89 Correct 18 ms 17512 KB Output is correct
90 Correct 24 ms 17308 KB Output is correct
91 Correct 20 ms 17236 KB Output is correct
92 Correct 21 ms 17304 KB Output is correct
93 Correct 21 ms 17288 KB Output is correct
94 Correct 24 ms 17224 KB Output is correct
95 Correct 21 ms 17236 KB Output is correct
96 Correct 586 ms 377320 KB Output is correct
97 Correct 25 ms 596 KB Output is correct
98 Correct 587 ms 377436 KB Output is correct
99 Correct 312 ms 377348 KB Output is correct
100 Correct 35 ms 596 KB Output is correct
101 Correct 549 ms 377364 KB Output is correct
102 Correct 572 ms 377404 KB Output is correct
103 Correct 549 ms 375924 KB Output is correct
104 Correct 548 ms 375944 KB Output is correct
105 Correct 542 ms 375868 KB Output is correct
106 Correct 579 ms 375924 KB Output is correct
107 Correct 547 ms 375920 KB Output is correct
108 Correct 415 ms 328636 KB Output is correct
109 Correct 566 ms 375980 KB Output is correct