zoukankan      html  css  js  c++  java
  • cf509C Sums of Digits

    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

    题意是给你一个递增序列的每个数的各位数字之和,求还原这个数列
    显然贪心,每次用比上一个数大的最小的那个就行了
    但是这题模拟实现太蛋疼了
    而且极限数据是一开始a[1]=300要凑出3后面33个9,然后a[2]到a[300]是299个1……这样只开300零几位的就死了
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<deque>
    #include<set>
    #include<map>
    #include<ctime>
    #define LL long long
    #define inf 0x7fffffff
    #define pa pair<int,int>
    #define pi 3.1415926535897932384626433832795028841971
    using namespace std;
    inline LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,len;
    int a[500],s[500],t[500];
    inline void calc(int x,int l)
    {
        memset(s,0,sizeof(s));
        len=0;x--;s[l]=1;
        while (x>=9&&len<l)
        {
            s[++len]=9;
            x-=9;
        }
        if (x)s[++len]+=x;
        len=l;
    }
    inline bool getmn(int x)
    {
        t[len+1]=0;
        for (int i=len;i>=1;i--)
            t[i]=t[i+1]+s[i];
        for (int i=1;i<=len;i++)
        {
            if (t[i]+1>x||s[i]==9)continue;
            s[i]++;
            while (x>9*(i-1)+t[i+1]+s[i]&&s[i]<9)s[i]++;
            if (x>9*(i-1)+t[i+1]+s[i])continue;
            x=x-(t[i+1]+s[i]);
            int now=1;
            while(now<i&&x>=9)
            {
                s[now++]=9;
                x-=9;
            }
            if (now!=i)s[now++]=x;
            for (int j=now;j<i;j++)s[j]=0;
            return 1;
        }
        return 0;
    }
    inline void put()
    {
        for (int i=len;i>=1;i--)
            printf("%d",s[i]);
        printf("
    ");
    }
    int main()
    {
        n=read();
        for (int i=1;i<=n;i++)a[i]=read();
        calc(a[1],a[1]/9+(a[1]%9!=0));
        put();
        
        for (int i=2;i<=n;i++)
        {
            if (a[i]>9*len)calc(a[i],a[i]/9+(a[i]%9!=0));
            if (!getmn(a[i]))calc(a[i],max(a[i]/9+(a[i]%9!=0),len+1));
            put();
        }
    }
    
    ——by zhber,转载请注明来源
  • 相关阅读:
    数据库课程设计心得【3】存储过程的运用
    看BBC研究大脑的科教片中“放松产生灵感”的笔记
    成功干掉“恶心的U盘自动运行病毒免疫目录”!共享方法,让更多的人干掉这东西!
    分享一大堆最新dot net asp.net c# 电子书下载 , 英文原版的。经典中的经典
    SQL学习之 对GROUP BY 和 HAVING 的理解 学习笔记
    关于Theme中.skin与css需要理清的关系
    最近的学习笔记,记录一些通俗易懂的学习类文章。更像是好资料参与索引。
    关于DNN Module开发学习以来的一点总结
    工具发布!QQ空间阅读与备份工具
    被忽视的大道理
  • 原文地址:https://www.cnblogs.com/zhber/p/4265710.html
Copyright © 2011-2022 走看看