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,转载请注明来源
  • 相关阅读:
    终于以一个ACMer的名义开通博客了。。
    Struts学习笔记一
    Matplotlib画图
    设计模式第一集——策略模式
    Hibernate学习笔记
    在linux下加python path【转】
    linux学习笔记
    C#3.0初体验
    asp.net中使用ffmpeg
    常用的正则表达式(经典)
  • 原文地址:https://www.cnblogs.com/zhber/p/4265710.html
Copyright © 2011-2022 走看看