답안 #585309

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585309 2022-06-28T15:12:28 Z _martynas Boarding Passes (BOI22_passes) C++11
60 / 100
2000 ms 25312 KB
#pragma optimize("O3,unroll-loops")
#pragma target("avx,avx2")
#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()
{
    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:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    1 | #pragma optimize("O3,unroll-loops")
      | 
passes.cpp:2: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    2 | #pragma target("avx,avx2")
      | 
passes.cpp: In function 'void init()':
passes.cpp:30:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |             for(int k = 0; k < A[i].size(); k++) {
      |                            ~~^~~~~~~~~~~~~
passes.cpp:31:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |                 while(r < A[j].size() && A[j][r] < A[i][k]) {
      |                       ~~^~~~~~~~~~~~~
passes.cpp: In function 'll cost(int, int, int)':
passes.cpp:64:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |         sum += split+1 < A[group].size() ? suff[group][g][split+1] : 0;
      |                ~~~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 596 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 4 ms 624 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 10 ms 596 KB Output is correct
6 Correct 15 ms 1680 KB Output is correct
7 Correct 15 ms 1632 KB Output is correct
8 Correct 15 ms 1680 KB Output is correct
9 Correct 14 ms 1680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 596 KB Output is correct
2 Correct 7 ms 596 KB Output is correct
3 Correct 32 ms 596 KB Output is correct
4 Correct 38 ms 596 KB Output is correct
5 Correct 20 ms 596 KB Output is correct
6 Correct 18 ms 596 KB Output is correct
7 Correct 31 ms 596 KB Output is correct
8 Correct 24 ms 624 KB Output is correct
9 Correct 28 ms 620 KB Output is correct
10 Correct 24 ms 596 KB Output is correct
11 Correct 25 ms 596 KB Output is correct
12 Correct 24 ms 604 KB Output is correct
13 Correct 27 ms 628 KB Output is correct
14 Correct 28 ms 596 KB Output is correct
15 Correct 25 ms 620 KB Output is correct
16 Correct 27 ms 624 KB Output is correct
17 Correct 26 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 596 KB Output is correct
2 Correct 7 ms 596 KB Output is correct
3 Correct 32 ms 596 KB Output is correct
4 Correct 38 ms 596 KB Output is correct
5 Correct 20 ms 596 KB Output is correct
6 Correct 18 ms 596 KB Output is correct
7 Correct 31 ms 596 KB Output is correct
8 Correct 24 ms 624 KB Output is correct
9 Correct 28 ms 620 KB Output is correct
10 Correct 24 ms 596 KB Output is correct
11 Correct 25 ms 596 KB Output is correct
12 Correct 24 ms 604 KB Output is correct
13 Correct 27 ms 628 KB Output is correct
14 Correct 28 ms 596 KB Output is correct
15 Correct 25 ms 620 KB Output is correct
16 Correct 27 ms 624 KB Output is correct
17 Correct 26 ms 596 KB Output is correct
18 Correct 7 ms 596 KB Output is correct
19 Correct 8 ms 596 KB Output is correct
20 Correct 33 ms 628 KB Output is correct
21 Correct 39 ms 596 KB Output is correct
22 Correct 22 ms 596 KB Output is correct
23 Correct 18 ms 616 KB Output is correct
24 Correct 29 ms 636 KB Output is correct
25 Correct 24 ms 620 KB Output is correct
26 Correct 27 ms 604 KB Output is correct
27 Correct 25 ms 596 KB Output is correct
28 Correct 26 ms 596 KB Output is correct
29 Correct 24 ms 624 KB Output is correct
30 Correct 27 ms 596 KB Output is correct
31 Correct 30 ms 596 KB Output is correct
32 Correct 25 ms 596 KB Output is correct
33 Correct 29 ms 616 KB Output is correct
34 Correct 27 ms 596 KB Output is correct
35 Correct 12 ms 744 KB Output is correct
36 Correct 13 ms 852 KB Output is correct
37 Correct 103 ms 2416 KB Output is correct
38 Correct 114 ms 2160 KB Output is correct
39 Correct 137 ms 2240 KB Output is correct
40 Correct 94 ms 2132 KB Output is correct
41 Correct 99 ms 2636 KB Output is correct
42 Correct 100 ms 2580 KB Output is correct
43 Correct 101 ms 2516 KB Output is correct
44 Correct 98 ms 2516 KB Output is correct
45 Correct 99 ms 2556 KB Output is correct
46 Correct 102 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 596 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 4 ms 624 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 10 ms 596 KB Output is correct
6 Correct 15 ms 1680 KB Output is correct
7 Correct 15 ms 1632 KB Output is correct
8 Correct 15 ms 1680 KB Output is correct
9 Correct 14 ms 1680 KB Output is correct
10 Correct 8 ms 596 KB Output is correct
11 Correct 7 ms 596 KB Output is correct
12 Correct 32 ms 596 KB Output is correct
13 Correct 38 ms 596 KB Output is correct
14 Correct 20 ms 596 KB Output is correct
15 Correct 18 ms 596 KB Output is correct
16 Correct 31 ms 596 KB Output is correct
17 Correct 24 ms 624 KB Output is correct
18 Correct 28 ms 620 KB Output is correct
19 Correct 24 ms 596 KB Output is correct
20 Correct 25 ms 596 KB Output is correct
21 Correct 24 ms 604 KB Output is correct
22 Correct 27 ms 628 KB Output is correct
23 Correct 28 ms 596 KB Output is correct
24 Correct 25 ms 620 KB Output is correct
25 Correct 27 ms 624 KB Output is correct
26 Correct 26 ms 596 KB Output is correct
27 Correct 7 ms 596 KB Output is correct
28 Correct 8 ms 596 KB Output is correct
29 Correct 33 ms 628 KB Output is correct
30 Correct 39 ms 596 KB Output is correct
31 Correct 22 ms 596 KB Output is correct
32 Correct 18 ms 616 KB Output is correct
33 Correct 29 ms 636 KB Output is correct
34 Correct 24 ms 620 KB Output is correct
35 Correct 27 ms 604 KB Output is correct
36 Correct 25 ms 596 KB Output is correct
37 Correct 26 ms 596 KB Output is correct
38 Correct 24 ms 624 KB Output is correct
39 Correct 27 ms 596 KB Output is correct
40 Correct 30 ms 596 KB Output is correct
41 Correct 25 ms 596 KB Output is correct
42 Correct 29 ms 616 KB Output is correct
43 Correct 27 ms 596 KB Output is correct
44 Correct 12 ms 744 KB Output is correct
45 Correct 13 ms 852 KB Output is correct
46 Correct 103 ms 2416 KB Output is correct
47 Correct 114 ms 2160 KB Output is correct
48 Correct 137 ms 2240 KB Output is correct
49 Correct 94 ms 2132 KB Output is correct
50 Correct 99 ms 2636 KB Output is correct
51 Correct 100 ms 2580 KB Output is correct
52 Correct 101 ms 2516 KB Output is correct
53 Correct 98 ms 2516 KB Output is correct
54 Correct 99 ms 2556 KB Output is correct
55 Correct 102 ms 2764 KB Output is correct
56 Correct 19 ms 624 KB Output is correct
57 Correct 59 ms 612 KB Output is correct
58 Correct 10 ms 636 KB Output is correct
59 Correct 4 ms 596 KB Output is correct
60 Correct 4 ms 596 KB Output is correct
61 Correct 5 ms 596 KB Output is correct
62 Correct 10 ms 596 KB Output is correct
63 Correct 15 ms 1640 KB Output is correct
64 Correct 16 ms 1680 KB Output is correct
65 Correct 15 ms 1680 KB Output is correct
66 Correct 14 ms 1680 KB Output is correct
67 Correct 10 ms 616 KB Output is correct
68 Correct 8 ms 596 KB Output is correct
69 Correct 33 ms 628 KB Output is correct
70 Correct 37 ms 612 KB Output is correct
71 Correct 20 ms 596 KB Output is correct
72 Correct 18 ms 624 KB Output is correct
73 Correct 28 ms 596 KB Output is correct
74 Correct 24 ms 624 KB Output is correct
75 Correct 27 ms 596 KB Output is correct
76 Correct 25 ms 624 KB Output is correct
77 Correct 25 ms 596 KB Output is correct
78 Correct 24 ms 596 KB Output is correct
79 Correct 26 ms 628 KB Output is correct
80 Correct 27 ms 608 KB Output is correct
81 Correct 25 ms 724 KB Output is correct
82 Correct 26 ms 624 KB Output is correct
83 Correct 26 ms 624 KB Output is correct
84 Correct 13 ms 752 KB Output is correct
85 Correct 12 ms 748 KB Output is correct
86 Correct 99 ms 2416 KB Output is correct
87 Correct 113 ms 2132 KB Output is correct
88 Correct 138 ms 2360 KB Output is correct
89 Correct 102 ms 2252 KB Output is correct
90 Correct 99 ms 2516 KB Output is correct
91 Correct 108 ms 2608 KB Output is correct
92 Correct 98 ms 2516 KB Output is correct
93 Correct 99 ms 2516 KB Output is correct
94 Correct 104 ms 2592 KB Output is correct
95 Correct 103 ms 2596 KB Output is correct
96 Correct 1224 ms 25312 KB Output is correct
97 Correct 66 ms 716 KB Output is correct
98 Correct 1251 ms 25300 KB Output is correct
99 Execution timed out 2087 ms 14632 KB Time limit exceeded
100 Halted 0 ms 0 KB -