[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.

输入示例


输出示例


数据规模及约定

见“输入

题解

显然是贪心,尽量使当前数接近上一个数,且大于等于上一个数 + 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的更多相关文章

  1. 【codeforces 509C】Sums of Digits

    [题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...

  2. cf509C Sums of Digits

    C. Sums of Digits time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

  4. Codeforces 509C Sums of Digits

    http://codeforces.com/contest/509/problem/C  题目大意: 给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方 ...

  5. Codeforces 509C Sums of Digits 贪心

    这道题目有人用DFS.有人用DP 我觉得还是最简单的贪心解决也是不错的选择. Ok,不废话了,这道题目的意思就是 原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Arra ...

  6. CodeForces 509C Sums of Digits(贪心乱搞)题解

    题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...

  7. Sums of Digits CodeForces - 509C (贪心,模拟)

    大意: 一个未知严格递增数组$a$, 给定每个数的数位和, 求$a[n]$最小的数组$a$ #include <iostream> #include <algorithm> # ...

  8. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. codeforces 509 D. Restoring Numbers(数学+构造)

    题目链接:http://codeforces.com/problemset/problem/509/D 题意:题目给出公式w[i][j]= (a[i] + b[j])% k; 给出w,要求是否存在这样 ...

随机推荐

  1. HTML5——行走日记

    效果展示: 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  2. iOS--隐藏和显示TabBar的方法

    1.隐藏TabBar: - (void)hideTabBar { if (self.tabBarController.tabBar.hidden == YES) { return; } UIView  ...

  3. 【C#】依赖于SharpZipLib的Zip压缩工具类

    上班第二天下班,课外作业,实现一个ZIP压缩的工具类.本来想用Package,但是写完了才发现不能解压其他工具压缩的zip包,比较麻烦,因此本工具类依赖了第三方的库(SharpZipLib  vers ...

  4. WPF--Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析

    原文地址: http://www.tuicool.com/articles/F7reem http://blog.csdn.net/yl2isoft/article/details/11711833 ...

  5. 【BZOJ 3732】 Network Kruskal重构树+倍增LCA

    Kruskal重构树裸题, Sunshine互测的A题就是Kruskal重构树,我通过互测了解到了这个神奇的东西... 理解起来应该没什么难度吧,但是我的Peaks连WA,,, 省选估计要滚粗了TwT ...

  6. 【BZOJ 3524】【Poi2014】Couriers 可持久化线段树

    为什么这个主席树叫可持久化线段树,我不知道,具体得问达神.我无限T,然后DaD3zZ一针见血地指出了我的N*50爆内存导致无限编译超时O)ZO)ZO)Z真是太神啦.以图为鉴: 达神题解传送门:http ...

  7. emberJS

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. knockout——官网demo

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 克隆选择算法-python实现

    CSAIndividual.py import numpy as np import ObjFunction class CSAIndividual: ''' individual of clone ...

  10. Spring Boot - fish

    1. @RestController combines @Controller and @ResponseBody, 这是不是意味着不用再import jakson的包(@ResponseBody时用 ...