现在的位置: 首页 > 综合 > 正文

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

2019年02月15日 ⁄ 综合 ⁄ 共 2879字 ⁄ 字号 评论关闭
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

先求出第一个数,这个数低位尽量放9

然后求其他的,先考虑当前的数是否可能和上一个数的位数一样,分两种:可能一样,从低位贪心找比上一个数大的,然后求出这个数;不可能一样:那么构造一个合法的,位数尽量少的数

/*************************************************************************
    > File Name: cf289c.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月01日 星期日 19时09分32秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
int pro[300];
string last;

void get_one (int sum)
{
	last = "";
	while (sum)
	{
		if (sum >= 9)
		{
			last += '9';
			sum -= 9;
		}
		else
		{
			last += '0' + sum;
			break;
		}
	}
	int len = last.size();
	for (int i = 0; i < len / 2; ++i)
	{
		swap (last[i], last[len - i - 1]);
	}
}

void get_lag (string now, int sum, int least)
{
	for (int i = 1; i <= least; ++i)
	{
		for (int j = 0; j < 10; ++j)
		{
			if (j == 0 && i == 1)
			{
				continue;
			}
			if (sum <= j + (least - i) * 9)
			{
				now += '0' + j;
				sum -= j;
				break;
			}
		}
	}
	last = now;
	cout << now << endl;
}

void get_equ_sma (string now, int sum)
{
	pro[0] = 0;
	int len = last.length();
	for (int i = 1; i <= len; ++i)
	{
		pro[i] = pro[i - 1] + last[i - 1] - '0';
	}
	int s = -1, ch;
	for (int i = len; i >= 1; --i)
	{
		bool flag = false;
		for (int j = last[i - 1] - '0' + 1; j <= 9; ++j)
		{
			if (sum - j - pro[i - 1] < 0)
			{
				continue;
			}
			int x = sum - j - pro[i - 1];
			int leave = x / 9;
			if (x % 9)
			{
				++leave;
			}
			if (leave <= (len - i))
			{
				flag = true;
				s = i;
				ch = j;
				break;
			}
		}
		if (flag)
		{
			break;
		}
	}
	if (s != -1)
	{
		for (int i = 1; i < s; ++i)
		{
			now += last[i - 1];
		}
		int leave = (sum - ch - pro[s - 1]);
		now += ch + '0';
		for (int i = s + 1; i <= len; ++i)
		{
			for (int j = 0; j < 10; ++j)
			{
				if (leave - j <= (len - i) * 9)
				{
					now += '0' + j;
					leave -= j;
					break;
				}
			}
		}
		cout << now << endl;
		last = now;
	}
	else
	{
		get_lag (now, sum, len + 1);
	}
}

void solve (int sum)
{
	string now = "";
	int least = sum / 9;
	if (sum % 9)
	{
		++least;
	}
	if (least > last.length())
	{
		get_lag (now, sum, least);
	}
	else
	{
		get_equ_sma (now, sum);
	}
}

int main ()
{
	int n;
	while (cin >> n)
	{
		int sum;
		cin >> sum;
		get_one (sum);
		cout << last << endl;
		for (int i = 2; i <= n; ++i)
		{
			cin >> sum;
			solve (sum);
		}
	}
	return 0;
}

抱歉!评论已关闭.