Submission #585300

# Submission time Handle Problem Language Result Execution time Memory
585300 2022-06-28T14:58:22 Z _martynas Boarding Passes (BOI22_passes) C++11
60 / 100
2000 ms 25272 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int MXG = 15;
const ll INF = 1e17;

string s;
int n;
vector<ll> A[MXG];
// pref[i][j][k] -> i after j cost from left at k
vector<ll> pref[MXG][MXG];
// suff[i][j][k] -> i after j cost from right at k
vector<ll> suff[MXG][MXG];
ll dp[1 << MXG];
bool visited[1 << MXG];

void init()
{
    // pref
    for(int i = 0; i < MXG; i++) {
        for(int j = 0; j < MXG; j++) {
            if(i == j || A[i].empty() || A[j].empty()) continue;
            int r = 0;
            int cost = 0;
            for(int k = 0; k < A[i].size(); k++) {
                while(r < A[j].size() && A[j][r] < A[i][k]) {
                    r++;
                }
                cost += r;
                pref[i][j].push_back(4*cost);
            }
        }
    }
    // suff
    for(int i = 0; i < MXG; i++) {
        for(int j = 0; j < MXG; j++) {
            if(i == j || A[i].empty() || A[j].empty()) continue;
            int r = A[j].size()-1;
            int cost = 0;
            for(int k = A[i].size()-1; k >= 0; k--) {
                while(r >= 0 && A[j][r] > A[i][k]) {
                    r--;
                }
                cost += A[j].size()-1-r;
                suff[i][j].insert(suff[i][j].begin(), 4*cost);
            }
        }
    }
}

ll cost(int group, int bitmask, int split)
{
    ll sum = 0;
    for(int g = 0; g < MXG; g++) {
        if(((bitmask >> g) & 1) == 0 || A[g].empty() || g == group) continue;
        //cerr << "left: " << (split > -1 ? pref[group][g][split] : 0) << "\n";
        sum += split > -1 ? pref[group][g][split] : 0;
        //cerr << "right: " << (split+1 < A[group].size() ? suff[group][g][split+1] : 0) << "\n";
        sum += split+1 < A[group].size() ? suff[group][g][split+1] : 0;
    }
    if(split > 0) {
        //cerr << "left chance: " << split*(split+1) << "\n";
        // int*int overflow LET'S GO!
        sum += 1LL*split*(split+1); // chance to collide between on the left
    }
    ll cnt = A[group].size() - (split+1);
    if(cnt > 1) {
        //cerr << "right chance: " << cnt*(cnt-1) << "\n";
        sum += cnt*(cnt-1); // chance to collide between on the right
    }
    return sum;
}

ll recur(int bitmask)
{
    if(bitmask == 0) {
        return 0;
    }
    if(visited[bitmask]) {
        return dp[bitmask];
    }
    visited[bitmask] = true;
    ll mn = INF;
    for(int g = 0; g < MXG; g++) {
        if(((bitmask >> g) & 1) == 0) continue;
        int bits = bitmask - (1 << g);
        ll initial = recur(bits);
        if(A[g].empty()) {
            mn = min(mn, initial);
            continue;
        }
        int l = -1, r = A[g].size()-1;
        while(l < r) {
            int mid = l == -1 && r == 0 ? -1 : (l + r) / 2;
            ll a = cost(g, bits, mid);
            ll b = cost(g, bits, mid+1);
            if(a < b) {
                r = mid;
            }
            else {
                l = mid+1;
            }
        }
        mn = min(mn, initial + cost(g, bits, l));
    }
    dp[bitmask] = mn;
    return dp[bitmask];
}

int main()
{
// For some reason I have to comment this:
// ============================
//    cin.tie(0);
//    ios::sync_with_stdio(0);
// ============================
    getline(cin, s);
    n = s.size();

    for(int i = 0; i < n; i++) {
        A[s[i]-'A'].push_back(i);
    }
    init();
    // the hell is this grader?
    ll ans = recur(0x7fff);
    cout << ans / 4;
    if(ans % 4 == 1) {
        cout << ".25";
    }
    else if(ans % 4 == 2) {
        cout << ".5";
    }
    else if(ans % 4 == 3) {
        cout << ".75";
    }

    return 0;
}
/*
AACCAA
HEHEHEHIHILOL
ONMLKJIHGFEDCBAABCDEFGHIJKLMNO
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

*/

Compilation message

passes.cpp: In function 'void init()':
passes.cpp:28:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |             for(int k = 0; k < A[i].size(); k++) {
      |                            ~~^~~~~~~~~~~~~
passes.cpp:29:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |                 while(r < A[j].size() && A[j][r] < A[i][k]) {
      |                       ~~^~~~~~~~~~~~~
passes.cpp: In function 'll cost(int, int, int)':
passes.cpp:62:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         sum += split+1 < A[group].size() ? suff[group][g][split+1] : 0;
      |                ~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 596 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 6 ms 600 KB Output is correct
5 Correct 11 ms 596 KB Output is correct
6 Correct 18 ms 1612 KB Output is correct
7 Correct 20 ms 1612 KB Output is correct
8 Correct 21 ms 1612 KB Output is correct
9 Correct 25 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 10 ms 604 KB Output is correct
3 Correct 46 ms 596 KB Output is correct
4 Correct 40 ms 596 KB Output is correct
5 Correct 22 ms 608 KB Output is correct
6 Correct 19 ms 604 KB Output is correct
7 Correct 32 ms 596 KB Output is correct
8 Correct 26 ms 604 KB Output is correct
9 Correct 34 ms 596 KB Output is correct
10 Correct 34 ms 596 KB Output is correct
11 Correct 32 ms 596 KB Output is correct
12 Correct 32 ms 596 KB Output is correct
13 Correct 30 ms 596 KB Output is correct
14 Correct 31 ms 596 KB Output is correct
15 Correct 28 ms 596 KB Output is correct
16 Correct 31 ms 596 KB Output is correct
17 Correct 32 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 10 ms 604 KB Output is correct
3 Correct 46 ms 596 KB Output is correct
4 Correct 40 ms 596 KB Output is correct
5 Correct 22 ms 608 KB Output is correct
6 Correct 19 ms 604 KB Output is correct
7 Correct 32 ms 596 KB Output is correct
8 Correct 26 ms 604 KB Output is correct
9 Correct 34 ms 596 KB Output is correct
10 Correct 34 ms 596 KB Output is correct
11 Correct 32 ms 596 KB Output is correct
12 Correct 32 ms 596 KB Output is correct
13 Correct 30 ms 596 KB Output is correct
14 Correct 31 ms 596 KB Output is correct
15 Correct 28 ms 596 KB Output is correct
16 Correct 31 ms 596 KB Output is correct
17 Correct 32 ms 604 KB Output is correct
18 Correct 9 ms 596 KB Output is correct
19 Correct 10 ms 596 KB Output is correct
20 Correct 37 ms 596 KB Output is correct
21 Correct 42 ms 596 KB Output is correct
22 Correct 22 ms 612 KB Output is correct
23 Correct 21 ms 596 KB Output is correct
24 Correct 33 ms 596 KB Output is correct
25 Correct 43 ms 640 KB Output is correct
26 Correct 32 ms 468 KB Output is correct
27 Correct 30 ms 596 KB Output is correct
28 Correct 35 ms 596 KB Output is correct
29 Correct 29 ms 596 KB Output is correct
30 Correct 32 ms 596 KB Output is correct
31 Correct 32 ms 596 KB Output is correct
32 Correct 28 ms 608 KB Output is correct
33 Correct 31 ms 596 KB Output is correct
34 Correct 30 ms 596 KB Output is correct
35 Correct 18 ms 760 KB Output is correct
36 Correct 17 ms 832 KB Output is correct
37 Correct 135 ms 2388 KB Output is correct
38 Correct 161 ms 2060 KB Output is correct
39 Correct 150 ms 2260 KB Output is correct
40 Correct 111 ms 2144 KB Output is correct
41 Correct 133 ms 2592 KB Output is correct
42 Correct 131 ms 2504 KB Output is correct
43 Correct 124 ms 2448 KB Output is correct
44 Correct 146 ms 2556 KB Output is correct
45 Correct 118 ms 2556 KB Output is correct
46 Correct 153 ms 2616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 596 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 6 ms 600 KB Output is correct
5 Correct 11 ms 596 KB Output is correct
6 Correct 18 ms 1612 KB Output is correct
7 Correct 20 ms 1612 KB Output is correct
8 Correct 21 ms 1612 KB Output is correct
9 Correct 25 ms 1612 KB Output is correct
10 Correct 9 ms 596 KB Output is correct
11 Correct 10 ms 604 KB Output is correct
12 Correct 46 ms 596 KB Output is correct
13 Correct 40 ms 596 KB Output is correct
14 Correct 22 ms 608 KB Output is correct
15 Correct 19 ms 604 KB Output is correct
16 Correct 32 ms 596 KB Output is correct
17 Correct 26 ms 604 KB Output is correct
18 Correct 34 ms 596 KB Output is correct
19 Correct 34 ms 596 KB Output is correct
20 Correct 32 ms 596 KB Output is correct
21 Correct 32 ms 596 KB Output is correct
22 Correct 30 ms 596 KB Output is correct
23 Correct 31 ms 596 KB Output is correct
24 Correct 28 ms 596 KB Output is correct
25 Correct 31 ms 596 KB Output is correct
26 Correct 32 ms 604 KB Output is correct
27 Correct 9 ms 596 KB Output is correct
28 Correct 10 ms 596 KB Output is correct
29 Correct 37 ms 596 KB Output is correct
30 Correct 42 ms 596 KB Output is correct
31 Correct 22 ms 612 KB Output is correct
32 Correct 21 ms 596 KB Output is correct
33 Correct 33 ms 596 KB Output is correct
34 Correct 43 ms 640 KB Output is correct
35 Correct 32 ms 468 KB Output is correct
36 Correct 30 ms 596 KB Output is correct
37 Correct 35 ms 596 KB Output is correct
38 Correct 29 ms 596 KB Output is correct
39 Correct 32 ms 596 KB Output is correct
40 Correct 32 ms 596 KB Output is correct
41 Correct 28 ms 608 KB Output is correct
42 Correct 31 ms 596 KB Output is correct
43 Correct 30 ms 596 KB Output is correct
44 Correct 18 ms 760 KB Output is correct
45 Correct 17 ms 832 KB Output is correct
46 Correct 135 ms 2388 KB Output is correct
47 Correct 161 ms 2060 KB Output is correct
48 Correct 150 ms 2260 KB Output is correct
49 Correct 111 ms 2144 KB Output is correct
50 Correct 133 ms 2592 KB Output is correct
51 Correct 131 ms 2504 KB Output is correct
52 Correct 124 ms 2448 KB Output is correct
53 Correct 146 ms 2556 KB Output is correct
54 Correct 118 ms 2556 KB Output is correct
55 Correct 153 ms 2616 KB Output is correct
56 Correct 23 ms 468 KB Output is correct
57 Correct 70 ms 596 KB Output is correct
58 Correct 13 ms 616 KB Output is correct
59 Correct 5 ms 596 KB Output is correct
60 Correct 6 ms 596 KB Output is correct
61 Correct 7 ms 596 KB Output is correct
62 Correct 15 ms 616 KB Output is correct
63 Correct 19 ms 1612 KB Output is correct
64 Correct 19 ms 1612 KB Output is correct
65 Correct 20 ms 1576 KB Output is correct
66 Correct 21 ms 1612 KB Output is correct
67 Correct 11 ms 600 KB Output is correct
68 Correct 10 ms 604 KB Output is correct
69 Correct 43 ms 596 KB Output is correct
70 Correct 39 ms 612 KB Output is correct
71 Correct 22 ms 596 KB Output is correct
72 Correct 20 ms 600 KB Output is correct
73 Correct 32 ms 596 KB Output is correct
74 Correct 29 ms 596 KB Output is correct
75 Correct 31 ms 596 KB Output is correct
76 Correct 29 ms 596 KB Output is correct
77 Correct 31 ms 604 KB Output is correct
78 Correct 29 ms 600 KB Output is correct
79 Correct 29 ms 596 KB Output is correct
80 Correct 30 ms 604 KB Output is correct
81 Correct 27 ms 600 KB Output is correct
82 Correct 29 ms 596 KB Output is correct
83 Correct 29 ms 596 KB Output is correct
84 Correct 14 ms 756 KB Output is correct
85 Correct 15 ms 756 KB Output is correct
86 Correct 119 ms 2388 KB Output is correct
87 Correct 145 ms 2104 KB Output is correct
88 Correct 144 ms 2216 KB Output is correct
89 Correct 105 ms 2068 KB Output is correct
90 Correct 115 ms 2592 KB Output is correct
91 Correct 123 ms 2468 KB Output is correct
92 Correct 117 ms 2452 KB Output is correct
93 Correct 110 ms 2516 KB Output is correct
94 Correct 113 ms 2552 KB Output is correct
95 Correct 116 ms 2568 KB Output is correct
96 Correct 1306 ms 25224 KB Output is correct
97 Correct 71 ms 600 KB Output is correct
98 Correct 1309 ms 25272 KB Output is correct
99 Execution timed out 2090 ms 14584 KB Time limit exceeded
100 Halted 0 ms 0 KB -