码迷,mamicode.com
首页 > 其他好文 > 详细

[codeforces 509]C. Sums of Digits

时间:2016-08-28 13:58:05      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

[codeforces 509]C. Sums of Digits

试题描述

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.

输入

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.

输出

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.

输入示例

3
3
2
1

输出示例

3
11
100

数据规模及约定

见“输入

题解

显然是贪心,尽量使当前数接近上一个数,且大于等于上一个数 + 1. 搞一个类似高精度运算的模拟。

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

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); }
    return x * f;
}

#define maxn 310
int n, B[maxn], num[maxn][maxn], len[maxn];

int main() {
	n = read();
	for(int i = 1; i <= n; i++) B[i] = read();
	
	len[0] = 1;
	for(int i = 1; i <= n; i++) {
		num[i-1][1]++;
		for(int j = 1; j <= len[i-1]; j++) {
			num[i-1][j+1] += num[i-1][j] / 10;
			num[i-1][j] %= 10;
		}
		for(int j = len[i-1] + 1; num[i-1][j]; j++) {
			num[i-1][j+1] += num[i-1][j] / 10;
			num[i-1][j] %= 10;
			len[i-1] = j;
		}
		int tot = B[i], s = 0;
		for(int j = 1; j <= len[i-1]; j++) s += num[i-1][j];
		if(s == tot) memcpy(num[i], num[i-1], sizeof(num[i-1])), len[i] = len[i-1];
		else {
			len[i] = 0;
			for(int j = len[i-1]; j; j--)
				if(tot > num[i-1][j]) {
					len[i] = max(len[i], j);
					num[i][j] = num[i-1][j];
					tot -= num[i][j];
				}
				else {
					len[i] = max(len[i], j + 1);
					num[i][j+1]++; tot--;
					for(int k = j; k; k--) num[i][k] = 0;
					break;
				}
			for(int j = 1; j <= len[i]; j++) {
				num[i][j+1] += num[i][j] / 10;
				num[i][j] %= 10;
			}
			for(int j = len[i] + 1; num[i][j]; j++) {
				num[i][j+1] += num[i][j] / 10;
				num[i][j] %= 10;
				len[i] = j;
			}
			tot = B[i];
			for(int j = 1; j <= len[i]; j++) tot -= num[i][j];
//			for(int j = len[i]; j; j--) printf("%d", num[i][j]); putchar(‘\n‘);
			for(int j = 1; tot; j++) {
//				printf("%d(%d) ", tot, num[i][j]);
				if(tot >= 9 - num[i][j]) tot -= (9 - num[i][j]), num[i][j] = 9;
				else num[i][j] += tot, tot = 0;
				len[i] = max(len[i], j);
			}
//			putchar(‘\n‘);
		}
		for(int j = len[i]; j; j--) printf("%d", num[i][j]); putchar(‘\n‘);
	}
	
	return 0;
}
/*
5
30
29
28
42
11
*/

 

[codeforces 509]C. Sums of Digits

标签:

原文地址:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/5814794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!