답안 #582107

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582107 2022-06-23T11:32:50 Z KoD Boarding Passes (BOI22_passes) C++17
100 / 100
205 ms 18880 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

using ll = long long;

template <class F> struct fixed : private F {
    explicit fixed(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class T>
constexpr T infty = std::numeric_limits<T>::max() / 2;

template <class T>
void setmin(T& x, const T& y) {
    if (x > y) {
        x = y;
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::string S;
    std::cin >> S;
    const int N = (int)S.size();
    const int G = 15;
    array<vector<int>, G> pos = {};
    for (int i = 0; i < N; ++i) {
        pos[S[i] - 'A'].push_back(i);
    }
    array<vector<int>, G> accum = {};
    accum.fill(vector(N + 1, 0));
    for (int i = 0; i < N; ++i) {
        for (int k = 0; k < G; ++k) {
            accum[k][i + 1] = accum[k][i];
        }
        accum[S[i] - 'A'][i + 1] += 1;
    }
    array<array<vector<ll>, G>, G> precalc = {};
    for (int i = 0; i < G; ++i) {
        for (int j = 0; j < G; ++j) {
            const int n = (int)pos[i].size();
            precalc[i][j].resize(n + 1);
            for (const int x : pos[i]) {
                precalc[i][j][0] += accum[j][N] - accum[j][x];
            }
            for (int k = 0; k < n; ++k) {
                const int x = pos[i][k];
                ll next = precalc[i][j][k];
                next -= accum[j][N] - accum[j][x];
                next += accum[j][x];
                precalc[i][j][k + 1] = next;
            }
        }
    }
    vector<ll> dp(1 << G, infty<ll>);
    dp[0] = 0;
    for (int set = 0; set < (1 << G); ++set) {
        for (int add = 0; add < G; ++add) {
            if (set >> add & 1) {
                continue;
            }
            const int n = pos[add].size();
            const auto calc = [&](const int k) {
                ll ret = 0;
                for (int i = 0; i < G; ++i) {
                    if (set >> i & 1) {
                        ret += precalc[add][i][k];
                    }
                }
                return ret * 2 + (ll)k * (k - 1) / 2 + (ll)(n - k) * (n - k - 1) / 2;
            };
            int l = -1, r = n + 1;
            while (r - l > 2) {
                const int m = (l + r) / 2;
                if (calc(m) > calc(m + 1)) {
                    l = m;
                } else {
                    r = m + 1;
                }
            }
            setmin(dp[set | (1 << add)], dp[set] + calc(l + 1));
        }
    }
    const ll ans = dp[(1 << G) - 1];
    std::cout << ans / 2;
    if (ans % 2 == 1) {
        std::cout << ".5";
    }
    std::cout << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 724 KB Output is correct
2 Correct 21 ms 468 KB Output is correct
3 Correct 20 ms 596 KB Output is correct
4 Correct 22 ms 596 KB Output is correct
5 Correct 24 ms 724 KB Output is correct
6 Correct 46 ms 14940 KB Output is correct
7 Correct 56 ms 17864 KB Output is correct
8 Correct 51 ms 18824 KB Output is correct
9 Correct 53 ms 18860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 596 KB Output is correct
2 Correct 24 ms 596 KB Output is correct
3 Correct 45 ms 596 KB Output is correct
4 Correct 44 ms 596 KB Output is correct
5 Correct 34 ms 596 KB Output is correct
6 Correct 37 ms 596 KB Output is correct
7 Correct 47 ms 596 KB Output is correct
8 Correct 36 ms 596 KB Output is correct
9 Correct 45 ms 596 KB Output is correct
10 Correct 37 ms 596 KB Output is correct
11 Correct 37 ms 596 KB Output is correct
12 Correct 37 ms 596 KB Output is correct
13 Correct 41 ms 640 KB Output is correct
14 Correct 46 ms 596 KB Output is correct
15 Correct 35 ms 596 KB Output is correct
16 Correct 36 ms 596 KB Output is correct
17 Correct 37 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 596 KB Output is correct
2 Correct 24 ms 596 KB Output is correct
3 Correct 45 ms 596 KB Output is correct
4 Correct 44 ms 596 KB Output is correct
5 Correct 34 ms 596 KB Output is correct
6 Correct 37 ms 596 KB Output is correct
7 Correct 47 ms 596 KB Output is correct
8 Correct 36 ms 596 KB Output is correct
9 Correct 45 ms 596 KB Output is correct
10 Correct 37 ms 596 KB Output is correct
11 Correct 37 ms 596 KB Output is correct
12 Correct 37 ms 596 KB Output is correct
13 Correct 41 ms 640 KB Output is correct
14 Correct 46 ms 596 KB Output is correct
15 Correct 35 ms 596 KB Output is correct
16 Correct 36 ms 596 KB Output is correct
17 Correct 37 ms 596 KB Output is correct
18 Correct 24 ms 596 KB Output is correct
19 Correct 26 ms 596 KB Output is correct
20 Correct 55 ms 596 KB Output is correct
21 Correct 46 ms 556 KB Output is correct
22 Correct 38 ms 596 KB Output is correct
23 Correct 33 ms 596 KB Output is correct
24 Correct 39 ms 596 KB Output is correct
25 Correct 37 ms 468 KB Output is correct
26 Correct 36 ms 596 KB Output is correct
27 Correct 38 ms 596 KB Output is correct
28 Correct 40 ms 596 KB Output is correct
29 Correct 44 ms 596 KB Output is correct
30 Correct 38 ms 596 KB Output is correct
31 Correct 40 ms 596 KB Output is correct
32 Correct 37 ms 568 KB Output is correct
33 Correct 38 ms 576 KB Output is correct
34 Correct 37 ms 596 KB Output is correct
35 Correct 35 ms 2388 KB Output is correct
36 Correct 27 ms 2388 KB Output is correct
37 Correct 86 ms 2388 KB Output is correct
38 Correct 86 ms 2388 KB Output is correct
39 Correct 40 ms 2388 KB Output is correct
40 Correct 86 ms 2388 KB Output is correct
41 Correct 81 ms 2388 KB Output is correct
42 Correct 94 ms 2388 KB Output is correct
43 Correct 106 ms 2368 KB Output is correct
44 Correct 102 ms 2388 KB Output is correct
45 Correct 86 ms 2388 KB Output is correct
46 Correct 80 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 724 KB Output is correct
2 Correct 21 ms 468 KB Output is correct
3 Correct 20 ms 596 KB Output is correct
4 Correct 22 ms 596 KB Output is correct
5 Correct 24 ms 724 KB Output is correct
6 Correct 46 ms 14940 KB Output is correct
7 Correct 56 ms 17864 KB Output is correct
8 Correct 51 ms 18824 KB Output is correct
9 Correct 53 ms 18860 KB Output is correct
10 Correct 23 ms 596 KB Output is correct
11 Correct 24 ms 596 KB Output is correct
12 Correct 45 ms 596 KB Output is correct
13 Correct 44 ms 596 KB Output is correct
14 Correct 34 ms 596 KB Output is correct
15 Correct 37 ms 596 KB Output is correct
16 Correct 47 ms 596 KB Output is correct
17 Correct 36 ms 596 KB Output is correct
18 Correct 45 ms 596 KB Output is correct
19 Correct 37 ms 596 KB Output is correct
20 Correct 37 ms 596 KB Output is correct
21 Correct 37 ms 596 KB Output is correct
22 Correct 41 ms 640 KB Output is correct
23 Correct 46 ms 596 KB Output is correct
24 Correct 35 ms 596 KB Output is correct
25 Correct 36 ms 596 KB Output is correct
26 Correct 37 ms 596 KB Output is correct
27 Correct 24 ms 596 KB Output is correct
28 Correct 26 ms 596 KB Output is correct
29 Correct 55 ms 596 KB Output is correct
30 Correct 46 ms 556 KB Output is correct
31 Correct 38 ms 596 KB Output is correct
32 Correct 33 ms 596 KB Output is correct
33 Correct 39 ms 596 KB Output is correct
34 Correct 37 ms 468 KB Output is correct
35 Correct 36 ms 596 KB Output is correct
36 Correct 38 ms 596 KB Output is correct
37 Correct 40 ms 596 KB Output is correct
38 Correct 44 ms 596 KB Output is correct
39 Correct 38 ms 596 KB Output is correct
40 Correct 40 ms 596 KB Output is correct
41 Correct 37 ms 568 KB Output is correct
42 Correct 38 ms 576 KB Output is correct
43 Correct 37 ms 596 KB Output is correct
44 Correct 35 ms 2388 KB Output is correct
45 Correct 27 ms 2388 KB Output is correct
46 Correct 86 ms 2388 KB Output is correct
47 Correct 86 ms 2388 KB Output is correct
48 Correct 40 ms 2388 KB Output is correct
49 Correct 86 ms 2388 KB Output is correct
50 Correct 81 ms 2388 KB Output is correct
51 Correct 94 ms 2388 KB Output is correct
52 Correct 106 ms 2368 KB Output is correct
53 Correct 102 ms 2388 KB Output is correct
54 Correct 86 ms 2388 KB Output is correct
55 Correct 80 ms 2388 KB Output is correct
56 Correct 30 ms 596 KB Output is correct
57 Correct 47 ms 596 KB Output is correct
58 Correct 25 ms 724 KB Output is correct
59 Correct 16 ms 596 KB Output is correct
60 Correct 23 ms 468 KB Output is correct
61 Correct 21 ms 468 KB Output is correct
62 Correct 25 ms 724 KB Output is correct
63 Correct 45 ms 14924 KB Output is correct
64 Correct 47 ms 17720 KB Output is correct
65 Correct 49 ms 18824 KB Output is correct
66 Correct 52 ms 18872 KB Output is correct
67 Correct 23 ms 468 KB Output is correct
68 Correct 24 ms 596 KB Output is correct
69 Correct 52 ms 596 KB Output is correct
70 Correct 42 ms 596 KB Output is correct
71 Correct 35 ms 596 KB Output is correct
72 Correct 30 ms 468 KB Output is correct
73 Correct 39 ms 596 KB Output is correct
74 Correct 35 ms 596 KB Output is correct
75 Correct 37 ms 596 KB Output is correct
76 Correct 36 ms 596 KB Output is correct
77 Correct 35 ms 596 KB Output is correct
78 Correct 36 ms 468 KB Output is correct
79 Correct 37 ms 596 KB Output is correct
80 Correct 39 ms 596 KB Output is correct
81 Correct 32 ms 596 KB Output is correct
82 Correct 36 ms 596 KB Output is correct
83 Correct 41 ms 596 KB Output is correct
84 Correct 31 ms 2388 KB Output is correct
85 Correct 29 ms 2388 KB Output is correct
86 Correct 87 ms 2388 KB Output is correct
87 Correct 83 ms 2388 KB Output is correct
88 Correct 38 ms 2388 KB Output is correct
89 Correct 81 ms 2388 KB Output is correct
90 Correct 86 ms 2388 KB Output is correct
91 Correct 88 ms 2388 KB Output is correct
92 Correct 101 ms 2388 KB Output is correct
93 Correct 86 ms 2388 KB Output is correct
94 Correct 84 ms 2388 KB Output is correct
95 Correct 80 ms 2516 KB Output is correct
96 Correct 171 ms 18880 KB Output is correct
97 Correct 48 ms 596 KB Output is correct
98 Correct 149 ms 18788 KB Output is correct
99 Correct 61 ms 18824 KB Output is correct
100 Correct 35 ms 596 KB Output is correct
101 Correct 159 ms 18872 KB Output is correct
102 Correct 168 ms 18740 KB Output is correct
103 Correct 157 ms 18812 KB Output is correct
104 Correct 162 ms 18876 KB Output is correct
105 Correct 178 ms 18880 KB Output is correct
106 Correct 165 ms 18836 KB Output is correct
107 Correct 159 ms 18856 KB Output is correct
108 Correct 205 ms 18852 KB Output is correct
109 Correct 170 ms 18760 KB Output is correct