[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. jQuery基础之(四)jQuery创建DOM元素

    利用DOM方法创建元素节点,通常要将document.createElement().document.createTextNode().appendChild()配合使用,十分麻烦. 而jQuery ...

  2. 第四章:Javascript表达式和运算符

    表达式是javascript中的一个短语,javascript解释器会将其计算出一个结果.程序中常用量是最简单的一类表达式就是变量.变量名也是一种简单的表达式,它的值就是赋值给变量的值.复杂的表达式是 ...

  3. 无法加载协定为“ServiceReference1.xxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分

    原因是config节点中有多个endpoint相同节点,提示按名称指示首选的终结点,说明程序不知道选那个节点. 解决办法,实例化service服务对象时,通过name值指定创建它. config文件部 ...

  4. 每天一个linux命令(37):free 命令

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  5. Daily Scrum – 1/6

    Meeting Minutes 确认修复了一个bug,即变方的: 分配了以后的任务: 确认将速度写入了用户的设置文件 Burndown     Progress   part 组员 今日工作 Time ...

  6. java web名词解释

    来源于:http://www.cnblogs.com/yxnchinahlj/archive/2012/02/24/2366110.html PO(persistant object) 持久对象 在o ...

  7. Could not open Hibernate Session for transaction;

    javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException: Co ...

  8. 给java应用打包

    C:\dollapp\classes> jar -cvf C:\dollapp\deploy\dollapp.jar *.* 上面的jar命令会把 C:\dollapp\classes 下的 所 ...

  9. nginx web加密访问

    有时我们会有这么一种需求,就是你的网站并不想提供一个公共的访问或者某些页面不希望公开, 我们希望的是某些特定的客户端可以访问.那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那 ...

  10. Java 缓存技术

    以下仅是对map对方式讨论.没有对点阵图阵讨论.作缓存要做以下2点:  1:清理及更新缓存时机的处理: . 虚拟机内存不足,清理缓存 .. 缓存时间超时,或访问次数超出, 启动线程更新 2:类和方法的 ...