답안 #1109456

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109456 2024-11-06T18:34:12 Z Kirill22 수열 (BOI14_sequence) C++17
100 / 100
183 ms 2044 KB
#include "bits/stdc++.h"
 
using namespace std;
 
bool contain(long long x, int c) {
    while (x > 0) {
        if (x % 10 == c) {
            return true;
        }
        x /= 10;
    }
    return false;
}
 
void solve() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        a[i] += (1 << x);
        b[i] = x;
    }
    long long ans = (long long) 1e18;
    auto check = [&] (long long x) {
        if (x >= ans) {
            return false;
        }
        for (int i = 0; i < n; i++) {
            if (!contain(x + i, b[i])) {
                return false;
            }
        }
        return true;
    };
    auto dfs = [&] (auto&& dfs, string tmp, vector<int> a) -> void {
        if ((int) tmp.size() >= 6 && (int) a.size() > 1) {
            return;
        }
        if ((int) a.size() == 1) {
            if (!a[0]) {
                std::reverse(tmp.begin(), tmp.end());
                long long res = stoll(tmp);
                if (res && check(res)) {
                    ans = res;
                }
                if (!tmp.empty() && tmp[0] == '0') {
                    std::reverse(tmp.begin(), tmp.end());
                    tmp.push_back('1');
                    std::reverse(tmp.begin(), tmp.end());
                    res = stoll(tmp);
                }
                if (res && check(res)) {
                    ans = res;
                }
                return;
            }
            string s = tmp;
            if (a[0] == 1) {
                a[0] = 3;
            }
            for (int i = 9; i > 0; i--) {
                if (a[0] >> i & 1) {
                    s.push_back((char) '0' + i);
                }
            }
            if (a[0] % 2) {
                char c = s.back();
                s.back() = '0';
                s.push_back(c);
            }
            std::reverse(s.begin(), s.end());
            long long res = stoll(s);
            if (res && check(res)) {
                ans = res;
            }
            return;
        }
        for (int x = 0; x < 10; x++) {
            vector<int> b = {0};
            int y = x;
            for (int i = 0; i < (int) a.size(); i++) {
                int msk = a[i];
                if (msk >> y & 1) {
                    msk -= (1 << y);
                }
                b.back() |= msk;
                y = (y + 1) % 10;
                if (y == 0 && i + 1 < (int) a.size()) {
                    b.push_back(0);
                }
            }
            string tmp2 = tmp;
            tmp2.push_back((char) '0' + x);
            dfs(dfs, tmp2, b);
        }
    };
    dfs(dfs, "", a);
    cout << ans << '\n';
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 6 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 4 ms 336 KB Output is correct
11 Correct 4 ms 464 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 6 ms 336 KB Output is correct
15 Correct 6 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 4 ms 460 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 5 ms 336 KB Output is correct
12 Correct 4 ms 336 KB Output is correct
13 Correct 4 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 5 ms 336 KB Output is correct
17 Correct 4 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 4 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 5 ms 472 KB Output is correct
23 Correct 8 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 19 ms 592 KB Output is correct
3 Correct 19 ms 592 KB Output is correct
4 Correct 23 ms 636 KB Output is correct
5 Correct 24 ms 592 KB Output is correct
6 Correct 15 ms 336 KB Output is correct
7 Correct 86 ms 1372 KB Output is correct
8 Correct 76 ms 1104 KB Output is correct
9 Correct 105 ms 2040 KB Output is correct
10 Correct 131 ms 1940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 98 ms 1104 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 3 ms 472 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 111 ms 1948 KB Output is correct
12 Correct 110 ms 1752 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 6 ms 336 KB Output is correct
15 Correct 4 ms 336 KB Output is correct
16 Correct 4 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 5 ms 456 KB Output is correct
20 Correct 5 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 2 ms 464 KB Output is correct
23 Correct 4 ms 336 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 5 ms 336 KB Output is correct
26 Correct 5 ms 468 KB Output is correct
27 Correct 19 ms 592 KB Output is correct
28 Correct 19 ms 592 KB Output is correct
29 Correct 23 ms 592 KB Output is correct
30 Correct 22 ms 592 KB Output is correct
31 Correct 15 ms 336 KB Output is correct
32 Correct 79 ms 1360 KB Output is correct
33 Correct 58 ms 1104 KB Output is correct
34 Correct 102 ms 2044 KB Output is correct
35 Correct 100 ms 1872 KB Output is correct
36 Correct 127 ms 1360 KB Output is correct
37 Correct 183 ms 1908 KB Output is correct
38 Correct 139 ms 1104 KB Output is correct
39 Correct 166 ms 1872 KB Output is correct
40 Correct 166 ms 1872 KB Output is correct