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


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

2016 acm/icpc 沈阳现场赛题解(5道题,更新ing)

5948.Thickest Burger(签到题)http://acm.hdu.edu.cn/showproblem.php?pid=5948题目大意:给你A和B,问2A+B和2B+A谁大?题目分析:...
  • cmershen
  • cmershen
  • 2016年11月21日 21:59
  • 1974

POJ 3067 Japan(树状数组求逆序对)

Japan http://poj.org/problem?id=3067 Time Limit: 1000MS Memory Limit: 65536K Descript...
  • synapse7
  • synapse7
  • 2013年07月27日 12:48
  • 1181

POJ3067:Japan(树状数组求逆序对)

Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for...
  • libin56842
  • libin56842
  • 2015年06月23日 21:38
  • 1993

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...
  • Guard_Mine
  • Guard_Mine
  • 2015年02月01日 20:34
  • 775

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

贪心,找比前一位大并且各位数字和为a[i]的最小的数。。。 #include #include #include #include #include #include #include ...
  • blankcqk
  • blankcqk
  • 2015年03月12日 19:10
  • 235

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

考虑每一位对答案的影响,然后三种情况分类讨论。。。 #include #include #include #include #include #include #include #in...
  • blankcqk
  • blankcqk
  • 2015年03月12日 20:11
  • 290

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

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

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...
  • Guard_Mine
  • Guard_Mine
  • 2015年02月01日 13:18
  • 1079

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...
  • Guard_Mine
  • Guard_Mine
  • 2015年02月01日 18:11
  • 874

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 ...
  • u014355480
  • u014355480
  • 2015年02月06日 19:44
  • 832
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
举报原因:
原因补充:

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