Submission #714434

#TimeUsernameProblemLanguageResultExecution timeMemory
714434StickfishSequence (BOI14_sequence)C++17
42 / 100
1086 ms2004 KiB
#include <iostream>
#include <bitset>
using namespace std;
using ll = long long;

const int MAXN = 1e5 + 123;
const ll INF = 1e18 + 177013;
int dgseq[MAXN];
bitset<10> dgs[MAXN];
bitset<10> dgsmod[MAXN];

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;
}

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 t = 0; t < m; ++t) {
        bitset<10> incl0;
        bitset<10> incl0mod;
        bitset<10> incl1;
        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[dgseq[k]] = 1;
        }
        //if (t == 0) {
        //cout << t << ' ' << incl0 << ' ' << incl1 << ": " << get_best_conseq(incl0, incl1) << endl;
        //cout << t << ' ' << incl0mod << ' ' << incl1 << ": " << get_best_conseq_nonzero(incl0mod, incl1) << endl;
        //}
        ans = min(ans, get_best_conseq(incl0, incl1) * m + t);
        ans = min(ans, get_best_conseq_nonzero(incl0mod, incl1) * m + t);
        //cout << get_best_conseq_nonzero(incl0mod, incl1) * m + t << endl;
    }

    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...