[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. (旧)子数涵数·PS ——素描效果

    一.准备素材(均为在百度上下载的) 二.打开ps,并在ps中打开第一张素材 三.复制图层(好习惯) 四.去色将图像变成黑白,图像->调整->去色,快捷键为Ctrl+Shift+U 五,复制 ...

  2. AngularJS开发指南12:AngularJS的模板,CSS,数据绑定详解

    模板 AngularJS模板是一种声明式的规则.它包含了模型和控制器的信息,最后会被渲染成用户在浏览器中看到的视图.它是静态的DOM,包含HTML,CSS和AngularJS指定的元素和属性.Angu ...

  3. 读代码之htmlParser

    在以前使用HtmlParser时,并未考虑过遇到org.htmlparser.tags之外的Tag怎么处理.直到碰到这样的一个标签,如果不加处理,HtmlParser无法对其进行处理.查阅自定义标签之 ...

  4. java多线程-Semaphore信号量使用

    介绍 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们能够正确.合理的使用公共资源. 概念 Semaphore分为单值和多值两种,前者 ...

  5. 在CentOS上安装SQLServer

    Install SQL Server on Red Hat Enterprise Linux 参考上面这篇文章即可,需要注意的是内容大于3.25G,然后设置Sa密码的时候需要至少一个大写字母.一个小写 ...

  6. Oracle查看锁表

     查看锁表进程SQL语句1: select sess.sid,     sess.serial#,     lo.oracle_username,     lo.os_user_name,     a ...

  7. Java编程思想学习(六) 多态

    1.Java语言的三大特性:继承.封装和多态. 继承:复用类的一种方法,可以简省很多代码: 封装:通过合并特征和行为来创建新的数据类型.[这种“数据类型”跟Java本身提供的8大“基本数据类型”的地位 ...

  8. Syntax error, annotations are only available if source level is 1.5

    在项目上右键 -> Properties -> Java Compiler

  9. PL/0编译器(java version) - Err.java

    1: package compiler; 2:   3: import java.io.BufferedWriter; 4:   5: public class Err { 6:   7: publi ...

  10. Ubuntu14.04编译安装mysql5.6.26

    Ubuntu14.04编译安装mysql5.6.26 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libncurses5-d ...