Submission #788170

# Submission time Handle Problem Language Result Execution time Memory
788170 2023-07-19T21:18:20 Z Ahmed57 Sequence (BOI14_sequence) C++17
100 / 100
561 ms 1428 KB
#include <bits/stdc++.h>

using namespace std;

long long ans = LLONG_MAX, pw[18];

void solve(vector<int> sections, long long curr = 0, int level = 0, bool had_zero = false) {
    if (curr > ans) return;
    int n = sections.size();

    if (n == 1) {
        int mask = sections[0];
        if (!mask) {
            if (!curr || had_zero) curr += pw[level];
        } else if (mask == 1) curr += pw[level + 1];
        else {
            if (mask & 1) {
                int mn = 10;
                for (int i = 9; i; i--) {
                    if (mask & (1 << i)) mn = i;
                }
                for (int i = 9; i; i--) {
                    if ((mask & (1 << i)) && i != mn) curr += i * pw[level++];
                }
                curr += mn * pw[++level];
            } else {
                for (int i = 9; i; i--) {
                    if (mask & (1 << i)) curr += i * pw[level++];
                }
            }
        }
        ans = min(ans, curr);
        return;
    }
    for(int i = 0;i<10;i++){
        vector<int> merged;
        int ins = 0;
        bool zero = false;

        int piv = i;
        for(int j = 0;j<n;j++){
            zero |= (piv == 0 && (sections[j] & 1));
            ins |= (sections[j] & (~(1 << piv)));
            piv++;
            if (piv == 10) {
                merged.push_back(ins);
                ins = piv = 0;
            }
        }
        if (piv) merged.push_back(ins);

        solve(merged, curr + pw[level] * i, level + 1, zero);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> sections(n);
    for(int i = 0;i<n;i++){
        int x;
        cin>>x;
        sections[i]=(1<<x);
    }
    pw[0] = 1;
    for(int i = 1;i<18;i++) pw[i] = 10 * pw[i - 1];
    solve(sections);
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 3 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 1 ms 316 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 3 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 3 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 5 ms 212 KB Output is correct
12 Correct 4 ms 212 KB Output is correct
13 Correct 1 ms 272 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 328 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 5 ms 212 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
22 Correct 6 ms 212 KB Output is correct
23 Correct 6 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 13 ms 340 KB Output is correct
3 Correct 13 ms 448 KB Output is correct
4 Correct 11 ms 448 KB Output is correct
5 Correct 17 ms 340 KB Output is correct
6 Correct 7 ms 340 KB Output is correct
7 Correct 48 ms 1108 KB Output is correct
8 Correct 38 ms 872 KB Output is correct
9 Correct 68 ms 1420 KB Output is correct
10 Correct 72 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 3 ms 324 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 98 ms 724 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 120 ms 1416 KB Output is correct
12 Correct 70 ms 1428 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 5 ms 212 KB Output is correct
15 Correct 3 ms 324 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 5 ms 212 KB Output is correct
24 Correct 2 ms 212 KB Output is correct
25 Correct 8 ms 212 KB Output is correct
26 Correct 6 ms 212 KB Output is correct
27 Correct 13 ms 340 KB Output is correct
28 Correct 13 ms 384 KB Output is correct
29 Correct 11 ms 336 KB Output is correct
30 Correct 16 ms 448 KB Output is correct
31 Correct 6 ms 340 KB Output is correct
32 Correct 47 ms 1108 KB Output is correct
33 Correct 43 ms 724 KB Output is correct
34 Correct 85 ms 1364 KB Output is correct
35 Correct 72 ms 1420 KB Output is correct
36 Correct 254 ms 1108 KB Output is correct
37 Correct 329 ms 1364 KB Output is correct
38 Correct 314 ms 944 KB Output is correct
39 Correct 547 ms 1412 KB Output is correct
40 Correct 561 ms 1404 KB Output is correct