제출 #11577

#제출 시각아이디문제언어결과실행 시간메모리
11577gs14004수열 (BOI14_sequence)C++98
67 / 100
1000 ms2144 KiB
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

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

void solve(vector<int> bit, long long cb, int depth, int had_zero){
    if(cb >= res) return;
    if(bit.size() == 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;
    }
    vector<int> v2;
    for (int i=0; i<10; i++) {
        int gz = 0;
        int piv = i;
        v2.clear();
        v2.push_back(0);
        for (int j=0; j<bit.size(); j++) {
            if(piv == 0 && (bit[j]&1)) gz = 1;
            v2.back() |= (bit[j] & (~(1<<piv)));
            piv++;
            if(piv == 10 && j != bit.size() - 1){
                v2.push_back(0);
                piv = 0;
            }
        }
        solve(v2,cb + p[depth] * i,depth+1,gz);
    }
}

int main(){
    vector<int> v;
    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);
        v.push_back(1<<t);
    }
    solve(v,0,0,0);
    printf("%lld",res);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...