Sums of Digits - CodeForces 509 C

Sums of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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


题意:给你一个递增序列的各位数之和,让你构造这个序列并使其最后一个数最小。

思路:使得最后一个数最小,就要使得每个数都尽可能的小。类似于数位dp吧,首先判断可行的位数,然后当前面一样时每次判断当前是否存在比上一个数大的方式,或者高位已经比前一个数大了,后面就要尽可能小。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int T,t,n,m;
int num[310],val[310][31000],len[310];
bool solve(int pos,int length,int p2)
{
    int i,j,k;
    for(i=length;i>0;i--)
    {
        if(p2>val[pos-1][i] && val[pos-1][i]!=9)
          return true;

        p2-=val[pos-1][i];
    }
    return false;
}
void dfs(int pos,int length1,int length2,int p,bool flag)
{
    if(length1==0)
      return;
    int p2;
    if(flag)
    {
        p2=max(0,p-(length1-1)*9);
        if(length1==len[pos])
          p2=max(p2,1);
        if(length1==1)
          p2=p;
        val[pos][length1]=p2;
        dfs(pos,length1-1,length2-1,p-p2,true);
    }
    else
    {
        p2=max(val[pos-1][length2],p-(length1-1)*9);
        if(length1==len[pos])
          p2=max(p2,1);
        if(length1==1)
          p2=p;
        if(p2>val[pos-1][length2])
        {
            val[pos][length1]=p2;
            dfs(pos,length1-1,length2-1,p-p2,true);
        }
        else if(solve(pos,length1-1,p-p2))
        {
            val[pos][length1]=p2;
            dfs(pos,length1-1,length2-1,p-p2,false);
        }
        else
        {
            p2++;
            val[pos][length1]=p2;
            dfs(pos,length1-1,length2-1,p-p2,true);
        }
    }
}
int main()
{
    int i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
       scanf("%d",&num[i]);
    len[0]=1;
    for(i=1;i<=n;i++)
    {
        if(len[i-1]+1<=(num[i]+8)/9)
        {
            dfs(i,(num[i]+8)/9,len[i-1],num[i],true);
            len[i]=(num[i]+8)/9;
        }
        else if(solve(i,len[i-1],num[i]))
        {
            len[i]=len[i-1];
            dfs(i,len[i-1],len[i-1],num[i],false);

        }
        else
        {
            len[i]=len[i-1]+1;
            dfs(i,len[i-1]+1,len[i-1],num[i],true);
        }
        for(j=len[i];j>=1;j--)
           printf("%d",val[i][j]);
        printf("\n");

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值