【codeforces 509C】Sums of Digits

【题目链接】:http://codeforces.com/contest/509/problem/C

【题意】

给你一个数组b[i]
要求一个严格升序的数组a[i];
使得a[i]是b[i]各个位上的数的和;
并且a[n]最小;

【题解】

每次处理的时候;
算出b[i]-b[i-1]的值
设为d
如果d>0
则从个位开始,不断地加1,直到d变成0;
如果个位变成9了,就加到十位,(这个时候个位的数字9不变,因为在低位,数字大一些,可以为后面高位的数字“分压”,后面的数字就能小一些了;这样整体数字就是最小的了;)然后十位如果小于9就加十位,…以此类推加百位,千位。。
如果d<=0
就先尝试减小数字,让d变成大于0的;
从个位开始;
把数字改为0->(这样满足数字最小,然后在一个适当的高位进一位,就会比前一个数字大了);
然后d+=这个位原来的数字;
直到d大于0;
然后这时记扫描到的位为第i位;
则在这一位i数字加一,然后d减小1;(如果这一位是9,那么再把d加上9然后找到一个<9的位置,在那一位再进位)
然后对于当前的数字;
相当于再加上d个数字,然后数字尽可能小;
这又变成d>0时的情况了;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 300+50;

int n,b[N],len;
int digits[10000];

void nex(int rest)
{
    for (int i = 1;rest;i++)
    {
        if (len<i) len = i;
        while (rest && digits[i]<9)
        {
            rest--;
            digits[i]++;
        }
    }
}

int main()
{
    //freopen("D:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false);
    rei(n);
    rep1(i,1,n)
        rei(b[i]);
    rep1(i,1,n)
    {
        int d = b[i]-b[i-1];
        if (d>0)
        {
            nex(d);
        }
        else
        {
            for (int i = 1; ;i++)
            {
                if (i>len) len = i;
                if (d>0 && digits[i]<9)
                {
                    d--;
                    digits[i]++;
                    nex(d);
                    break;
                }
                d+=digits[i];
                digits[i] = 0;
            }
        }
        rep2(i,len,1)
            pri(digits[i]);
        pri(endl);
    }
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(97)  评论(0编辑  收藏  举报