답안 #714434

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714434 2023-03-24T11:57:29 Z Stickfish 수열 (BOI14_sequence) C++17
42 / 100
1000 ms 2004 KB
#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';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 3 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 324 KB Output is correct
14 Correct 3 ms 212 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 2 ms 308 KB Output is correct
20 Correct 3 ms 212 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
22 Correct 4 ms 312 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Execution timed out 1071 ms 1876 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 260 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Execution timed out 1086 ms 2004 KB Time limit exceeded
6 Halted 0 ms 0 KB -