Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits

18 篇文章 0 订阅
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
题意很简单,给n个数字,要求n个数单调递增,且每个数的数字和为对应的这n个数!
这题150行的代码写了1个多小时,太差了,原理,就是从找比第n个数大1的数且位数和为第i个原始位数和pri[i],如果当前要求的位数和比上一位位数和要大,则尽量从右向左每一位加到9,再向前加就得出结果,如果,当前要求的位数比上一位位数和要小,则尽量把底位置为0,还是达不到的话,说明比第一位
还要小,就直接向前进一位就可以了,总位数不会超过300个,因为最大值也才为300!
#define INF			9000000000
#define EPS			(double)1e-9
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif
#define N 305
#define M 1005
#define maxn 205
#define MOD 1000000000000000007
int n,pri[N],goal[M],goallen,goalNum,ans[M],anslen,ansNum;
void FindNext(){
    int sum = 0,nextNum ;
    anslen = 0;
    if(ansNum == goalNum){
        anslen = goallen;
        FI(goallen)
        ans[i] = goal[i];
    }
    else if(ansNum > goalNum){
        nextNum = ansNum - goalNum;
        FI(goallen){
            sum = 9-goal[i];
            if(nextNum <= sum){
                ans[i] = goal[i] + nextNum;
                anslen = goallen;
                for(int j = i+1;j<anslen;j++){
                    ans[j] = goal[j];
                }
                nextNum = 0;
                break;
            }
            else {
                nextNum -= sum;
                ans[i] = 9;
            }
        }
        anslen = goallen;
        while(nextNum > 0){
            if(nextNum >=9){
                ans[anslen++] = 9;
                nextNum -= 9;
            }
            else {
                ans[anslen++] = nextNum;
                nextNum = 0;
            }
        }
    }
    else {
        bool isFind = false;
        nextNum = goalNum - ansNum;
        sum = -1;
        FI(goallen - 1){
            sum+=goal[i];
            if(goal[i+1] == 9)
                continue;
            if(nextNum <= sum){
                anslen = goallen;
                for(int j = goallen;j>i;j--){
                    ans[j] = goal[j];
                }
                ans[i+1] = goal[i+1] + 1;
                for(int j = i;j>=0;j--){
                    ans[j] = 0;
                }
                int temps = 0;
                for(int k=0;k<anslen;k++){
                    temps+=ans[k];
                }
                if(temps <= ansNum){
                    int ansi = 0;
                    temps = ansNum - temps;
                    while(temps > 0){
                        if(temps >=9){
                            ans[ansi++] = 9;
                            temps -= 9;
                        }
                        else {
                            ans[ansi++] = temps;
                            temps = 0;
                        }
                    }
                }
                isFind = true;
                break;
            }
        }
        if(!isFind){
            anslen = goallen+1;
            FI(anslen)
            ans[i] = 0;
            int tempNum = ansNum,tlen = 0;
            while(tempNum){
                if(tempNum >=9){
                    ans[tlen++] = 9;
                    tempNum -= 9;
                }
                else
                {
                    ans[tlen++] = tempNum;
                    tempNum = 0;
                }
            }
            if(tlen < anslen){
                ans[tlen-1]--;
                ans[anslen - 1] = 1;
            }
        }
    }
    goallen = anslen;
    FI(anslen){
        goal[i] = ans[i];
    }
    goal[0]++;
    FI(anslen){
        if(goal[i] >= 10){
            if(i == anslen - 1){
                goal[i+1] = 0;
                goallen++;
            }
            goal[i+1] += 1;
            goal[i] %= 10;

        }
    }
    goalNum = 0;
    FI(goallen){
        goalNum += goal[i];
    }
}
int main()
{
    while(S(n)!=EOF)
    {
        FI(n){
            S(pri[i]);
        }
        memset(goal,0,sizeof(goal));
        memset(ans,0,sizeof(ans));
        goallen = 1;anslen = 0;goalNum = 0;
        FI(n){
            ansNum = pri[i];
            FindNext();
            FJ(anslen){
                printf("%d",ans[anslen-1-j]);
            }
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值