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

原创 2015年07月07日 23:55:13
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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

C. Sums of Digits time limit per test 2 seconds memory limit per test 256 megabytes input...

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

贪心,找比前一位大并且各位数字和为a[i]的最小的数。。。 #include #include #include #include #include #include #include ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song

考虑每一位对答案的影响,然后三种情况分类讨论。。。 #include #include #include #include #include #include #include #in...

Codeforces Round #289 (Div. 2, ACM ICPC Rules)

一月最后一场,ACM-ICPC规则,没有pretest,没有hack,原本感觉会比上次简单点。。结果把自己打成绿名了。。。 一共就出了一道题。结束之后,B题改过了。 509A - Maximum in...

Codeforces Round #289 (Div. 2, ACM ICPC Rules) B. Painting Pebbles

B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes inpu...

Codeforces Round #289 (Div. 2, ACM ICPC Rules)

Codeforces Round #289 (Div. 2, ACM ICPC Rules)详细题解!!!!!

Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song

E. Pretty Song time limit per test 1 second memory limit per test 256 megabytes input st...

Codeforces Round #289 (Div. 2, ACM ICPC Rules) -- B. Painting Pebbles (贪心)

B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes ...

Codeforces Round #145 (Div. 2, ACM-ICPC Rules)

编码能力实在太弱了....这次的cf前五道全是implementation问题,竟然在三个半小时内敲不出来... zigbee也让人头疼,哎... A Lefthan...

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E - Cubes

#include #include #include #include #include #include #include #include #include #include #include #...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)