Submission #716939

# Submission time Handle Problem Language Result Execution time Memory
716939 2023-03-31T12:43:41 Z Stickfish Sequence (BOI14_sequence) C++17
100 / 100
183 ms 5716 KB
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
using ll = long long;

const int MAXN = 1e5 + 123;
const ll INF = 1e18 + 177013;

bool in(int x, int n) {
    while (n > 0) {
        if (n % 10 == x)
            return true;
        n /= 10;
    }
    return false;
}

ll get_best(bitset<10> incl) {
    ll ans = 0;
    for (int t = 1; t < 10; ++t) {
        if (!incl[t])
            continue;
        ans *= 10;
        ans += t;
        if (incl[0]) {
            ans *= 10;
            incl[0] = 0;
        }
    }
    if (ans == 0 && incl[0])
        return 10;
    return ans;
}

ll get_best_nonzero(bitset<10> incl) {
    return max(1ll, get_best(incl));
}

ll get_best_conseq(bitset<10> incl0, bitset<10> incl1) {
    if (incl0 == 0 && incl1 == 0)
        return 0;
    //cout << "A" << endl;
    ll ans = INF;
    for (int d = 0; d < 10; ++d) {
        bitset<10> nincl0 = incl0;
        bitset<10> nincl1 = incl1;
        nincl0[d] = nincl1[(d + 1) % 10] = 0;
        if (d == 0 && incl0[0]) {
            ans = min(ans, get_best_nonzero(nincl0 | nincl1) * 10);
            ans = min(ans, get_best(incl0 | nincl1) * 10);
        } else if (d == 9 && (incl0[9] || incl1[0])) {
            ans = min(ans, get_best_conseq(nincl0, nincl1) * 10 + 9);
        } else {
            ans = min(get_best(nincl0 | nincl1) * 10 + d, ans);
        }
    }
    //if (incl0 == 0 && incl1.count() == 1 && incl1[1]) {
        //cout << "HYPE " << ans << endl;
    //}
    //cout << "B" << endl;
    return ans;
}

ll get_best_conseq_nonzero(bitset<10> incl0, bitset<10> incl1) {
    ll ans = get_best_conseq(incl0, incl1);
    if (ans > 0)
        return ans;
    if (incl0 == 0 && incl1 == 0)
        return 1;
    return 10;
}

int dgseq[MAXN];
bitset<10> dgs[MAXN];
bitset<10> dgsmod[MAXN];

bitset<10> incl0[MAXN];
bitset<10> incl0mod[MAXN];
bitset<10> incl1[MAXN];

signed main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> dgseq[i];
    int m = 10;
    while (m < n)
        m *= 10;
    for (int t = 0; t < m; ++t) { 
        int x = t;
        while (x > 0) {
            dgs[t][x % 10] = 1;
            x /= 10;
        }
        dgsmod[t] = dgs[t];
        if (t * 10 < m)
            dgsmod[t][0] = 1;
    }
    ll ans = INF;
    for (int d = 0; d < 10; ++d) {
        vector<int> rdgseq;
        for (int t = m - 1; t >= 0; --t) {
            if (m - t - 1 < n && dgseq[m - t - 1] == d) {
                rdgseq.push_back(m - t - 1);
                swap(rdgseq.back(), rdgseq[rand() % rdgseq.size()]);
            }
            for (auto k : rdgseq) {
                if (!dgs[t + k][d]) {
                    incl0[t][d] = 1;
                    break;
                }
            }
            for (auto k : rdgseq) {
                if (!dgsmod[t + k][d]) {
                    incl0mod[t][d] = 1;
                    break;
                }
            }
        }
    }
    for (int d = 0; d < 10; ++d) {
        vector<int> rdgseq;
        for (int t = 0; t < m; ++t) {
            if (m - t < n && dgseq[m - t] == d) {
                rdgseq.push_back(m - t);
                swap(rdgseq.back(), rdgseq[rand() % rdgseq.size()]);
            }
            for (auto k : rdgseq) {
                if (!dgsmod[t + k - m][d]) {
                    incl1[t][d] = 1;
                    break;
                }
            }
        }
    }
    for (int t = 0; t < m; ++t) {
        //for (int k = 0; k < n && t + k < m; ++k) {
            //if (!dgs[t + k][dgseq[k]])
                //incl0[dgseq[k]] = 1;
            //if (!dgsmod[t + k][dgseq[k]])
                //incl0mod[dgseq[k]] = 1;
        //}
        //for (int k = m - t; k < n; ++k) {
            //if (!dgsmod[t + k - m][dgseq[k]])
                //incl1[t][dgseq[k]] = 1;
        //}
        ans = min(ans, get_best_conseq(incl0[t], incl1[t]) * m + t);
        ans = min(ans, get_best_conseq_nonzero(incl0mod[t], incl1[t]) * m + t);
    }

    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 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 1 ms 212 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 304 KB Output is correct
15 Correct 2 ms 300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 244 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 2 ms 308 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 312 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 308 KB Output is correct
18 Correct 2 ms 304 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 312 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 136 ms 3528 KB Output is correct
3 Correct 135 ms 3436 KB Output is correct
4 Correct 104 ms 3616 KB Output is correct
5 Correct 105 ms 3612 KB Output is correct
6 Correct 5 ms 724 KB Output is correct
7 Correct 47 ms 5160 KB Output is correct
8 Correct 152 ms 4304 KB Output is correct
9 Correct 51 ms 5700 KB Output is correct
10 Correct 52 ms 5664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 101 ms 4144 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 51 ms 5644 KB Output is correct
12 Correct 183 ms 5632 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 304 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 312 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 308 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 152 ms 3660 KB Output is correct
28 Correct 135 ms 3516 KB Output is correct
29 Correct 95 ms 3604 KB Output is correct
30 Correct 90 ms 3520 KB Output is correct
31 Correct 5 ms 724 KB Output is correct
32 Correct 48 ms 5152 KB Output is correct
33 Correct 151 ms 4304 KB Output is correct
34 Correct 50 ms 5716 KB Output is correct
35 Correct 51 ms 5692 KB Output is correct
36 Correct 125 ms 4620 KB Output is correct
37 Correct 107 ms 4908 KB Output is correct
38 Correct 135 ms 4280 KB Output is correct
39 Correct 126 ms 4896 KB Output is correct
40 Correct 117 ms 4800 KB Output is correct