Submission #11578

# Submission time Handle Problem Language Result Execution time Memory
11578 2014-12-02T14:22:44 Z gs14004 Sequence (BOI14_sequence) C++
100 / 100
432 ms 33040 KB
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int n;
long long res = 1e16, p[17];

void solve(int *bit, int sz, long long cb, int depth, int had_zero){
    if(cb >= res) return;
    if(sz == 1){
        int r = bit[0];
        if(r == 0){
            if(cb == 0 || had_zero) cb += p[depth];
        }
        else if(r == 1){
            cb += p[depth] * 10;
        }
        else{
            if(r&1){
                int minima = 1e9;
                for (int i=9; i; i--) {
                    if((r>>i)&1) minima = i;
                }
                for (int i=9; i; i--) {
                    if((r>>i)&1 && i != minima){
                        cb += p[depth] * i;
                        depth++;
                    }
                }
                depth++;
                cb += p[depth] * minima;
            }
            else{
                for (int i=9; i; i--) {
                    if((r>>i)&1){
                        cb += p[depth] * i;
                        depth++;
                    }
                }
            }
        }
        res = min(res,cb);
        return;
    }
    int* t = (int*) malloc(sizeof(int) * (sz/10 + 2));
    for (int i=0; i<10; i++) {
        int gz = 0;
        int nz = 0;
        int piv = i;
        t[nz] = 0;
        for (int j=0; j<sz; j++) {
            if(piv == 0 && (bit[j]&1)) gz = 1;
            t[nz] |= (bit[j] & (~(1<<piv)));
            piv++;
            if(piv == 10 && j != sz - 1){
                t[++nz] = 0;
                piv = 0;
            }
        }
        solve(t,nz+1,cb + p[depth] * i,depth+1,gz);
    }
}

int main(){
    int a[100005];
    scanf("%d",&n);
    p[0] = 1;
    for (int i=1; i<17; i++) {
        p[i] = p[i-1] * 10;
    }
    for (int i=0; i<n; i++) {
        int t;
        scanf("%d",&t);
        a[i] = 1<<t;
    }
    solve(a,n,0,0,0);
    printf("%lld",res);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1356 KB Output is correct
2 Correct 0 ms 1356 KB Output is correct
3 Correct 0 ms 1356 KB Output is correct
4 Correct 0 ms 1360 KB Output is correct
5 Correct 0 ms 1356 KB Output is correct
6 Correct 0 ms 1360 KB Output is correct
7 Correct 0 ms 1352 KB Output is correct
8 Correct 0 ms 1356 KB Output is correct
9 Correct 0 ms 1356 KB Output is correct
10 Correct 0 ms 1356 KB Output is correct
11 Correct 0 ms 1356 KB Output is correct
12 Correct 0 ms 1356 KB Output is correct
13 Correct 0 ms 1352 KB Output is correct
14 Correct 0 ms 1356 KB Output is correct
15 Correct 0 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1356 KB Output is correct
2 Correct 0 ms 1356 KB Output is correct
3 Correct 0 ms 1356 KB Output is correct
4 Correct 0 ms 1356 KB Output is correct
5 Correct 0 ms 1352 KB Output is correct
6 Correct 0 ms 1360 KB Output is correct
7 Correct 0 ms 1356 KB Output is correct
8 Correct 0 ms 1356 KB Output is correct
9 Correct 0 ms 1356 KB Output is correct
10 Correct 0 ms 1352 KB Output is correct
11 Correct 0 ms 1488 KB Output is correct
12 Correct 0 ms 1356 KB Output is correct
13 Correct 0 ms 1356 KB Output is correct
14 Correct 0 ms 1352 KB Output is correct
15 Correct 0 ms 1360 KB Output is correct
16 Correct 0 ms 1360 KB Output is correct
17 Correct 0 ms 1356 KB Output is correct
18 Correct 0 ms 1352 KB Output is correct
19 Correct 0 ms 1356 KB Output is correct
20 Correct 4 ms 1616 KB Output is correct
21 Correct 0 ms 1356 KB Output is correct
22 Correct 4 ms 1616 KB Output is correct
23 Correct 4 ms 1620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1352 KB Output is correct
2 Correct 8 ms 2016 KB Output is correct
3 Correct 4 ms 2012 KB Output is correct
4 Correct 4 ms 2016 KB Output is correct
5 Correct 8 ms 2280 KB Output is correct
6 Correct 4 ms 1620 KB Output is correct
7 Correct 32 ms 3860 KB Output is correct
8 Correct 24 ms 3076 KB Output is correct
9 Correct 52 ms 4916 KB Output is correct
10 Correct 56 ms 4920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1356 KB Output is correct
2 Correct 0 ms 1356 KB Output is correct
3 Correct 0 ms 1360 KB Output is correct
4 Correct 0 ms 1352 KB Output is correct
5 Correct 72 ms 7036 KB Output is correct
6 Correct 0 ms 1352 KB Output is correct
7 Correct 0 ms 1352 KB Output is correct
8 Correct 0 ms 1356 KB Output is correct
9 Correct 0 ms 1356 KB Output is correct
10 Correct 0 ms 1356 KB Output is correct
11 Correct 84 ms 8088 KB Output is correct
12 Correct 56 ms 4916 KB Output is correct
13 Correct 0 ms 1356 KB Output is correct
14 Correct 0 ms 1484 KB Output is correct
15 Correct 0 ms 1356 KB Output is correct
16 Correct 0 ms 1352 KB Output is correct
17 Correct 0 ms 1356 KB Output is correct
18 Correct 0 ms 1360 KB Output is correct
19 Correct 0 ms 1352 KB Output is correct
20 Correct 0 ms 1356 KB Output is correct
21 Correct 0 ms 1352 KB Output is correct
22 Correct 0 ms 1360 KB Output is correct
23 Correct 4 ms 1620 KB Output is correct
24 Correct 0 ms 1356 KB Output is correct
25 Correct 4 ms 1616 KB Output is correct
26 Correct 4 ms 1620 KB Output is correct
27 Correct 8 ms 2012 KB Output is correct
28 Correct 8 ms 2012 KB Output is correct
29 Correct 0 ms 2016 KB Output is correct
30 Correct 4 ms 2284 KB Output is correct
31 Correct 4 ms 1620 KB Output is correct
32 Correct 32 ms 3864 KB Output is correct
33 Correct 28 ms 3076 KB Output is correct
34 Correct 52 ms 4920 KB Output is correct
35 Correct 56 ms 4916 KB Output is correct
36 Correct 200 ms 16272 KB Output is correct
37 Correct 256 ms 19964 KB Output is correct
38 Correct 236 ms 19440 KB Output is correct
39 Correct 412 ms 33040 KB Output is correct
40 Correct 432 ms 33036 KB Output is correct