Codeforces Round #289 C. Sums of Digits(构造)

Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.

Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.

It is guaranteed that such a sequence always exists.

Input

The first line contains a single integer number n (1 ≤ n ≤ 300).

Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.

Output

Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi.

If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.

Sample test(s)
Input
3
1
2
3
Output
1
2
3
Input
3
3
2
1
Output
3
11
100

                                                                

题意:

题目给出n个数,表示为每一个数的位数和,要求得到的数列是从大到小且都最小的。

思路:

根据上一个数来构造下一个数,先根据上一个数确定高位,使得此时的数比上一个数大,接着从最低位开始填数,在数位和的范围内尽可能的填。

分为两种情况:1‘上一个和大于下一个和,则从高位开始遍历,直到小于数位和的最低位,此位增加1(在不是9的情况下),接着从最低位开始赋值,尽可能大的赋值。2’上一个和小于下一个和,从最低位开始尽可能大的赋值。

CODE:

#include 
#include 
#include 

using namespace std;
const int maxn = 305;
int N, n[maxn];
int ans[maxn][1005], cnt[maxn];

void getnum(int n1, int i)
{
    int ai = 0;
    while(n1 > 0) {
        ans[i][ai++] = n1-9 >= 0? 9: n1;
        n1 -= 9;
    }
}

int main()
{  // freopen("in", "r", stdin);

    while(~scanf("%d", &N)) {
        memset(ans, 0, sizeof(ans));
        for(int i = 0; i < N; ++i) {
            scanf("%d", &n[i]);
        }
        int sum;
        getnum(n[0], 0);
         for(int i = 1; i <= N; ++i) {

            for(int k = 1000; k >= 0; --k) {
                if(ans[i-1][k]) {
                    cnt[i-1] = k;
                    if(i == N) break;
                    sum = 0;
                    for(int j = k; j >= 0; --j){

                        if(sum + ans[i-1][j] >= n[i]) {
                            j++;
                            while(ans[i-1][j] == 9) {
                                sum -= ans[i-1][j];
                                ans[i][j] = 0;
                                j++;
                            }

                            ans[i][j] = ans[i-1][j] + 1;

                            int s = n[i] - sum - 1;
                            //if(i == 45) printf("s = %d\n", s);
                            if(s > 0) {
                                getnum(s, i);
                            }
                            sum = sum + s + 1;
                            break;
                        }
                        else {
                            sum += ans[i-1][j];
                            ans[i][j] = ans[i-1][j];
                        }
                    }
                    break;
                }
            }

            if(n[i] - n[i-1]) {
                int x = n[i] - n[i-1];
                int ai = 0;
                while(x > 0) {
                    if(9 - ans[i][ai] <= x) {
                        x = x - (9 - ans[i][ai]);
                        ans[i][ai] = 9;
                    }
                    else {
                        ans[i][ai] += x;
                        x -= x;
                    }
                    ai++;
                }

            }
        }

        for(int i = 0; i < N; ++i) {
            for(int j = cnt[i]; j >= 0; --j) {
                printf("%d", ans[i][j]);
            }
            printf("\n");
        }

    }
    return 0;
}





你可能感兴趣的:(构造)