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

本文介绍了一种算法,用于根据给定的数列构建一个严格递增的整数数列,其中每个数的十进制表示的数字之和等于给定数列中对应的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <iostream>
#include <cstdio>
#include <cstring>

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;
}





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值