[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. mysql基础 事务的认识和使用

    事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务是恢复和并发控制的基本单位. 在关系数据库中,一个事务可以是一条SQL语句,一组SQL语句或整个程序 ...

  2. impdp使用

    创建一个dir,dump_dir1,将备份文件放在下面 impdp szfetsc_card/123456 directory=dump_dir1 dumpfile=130912.dmp REMAP_ ...

  3. DOM(八)使用DOM控制表单

    1.表单简介 表单<form>是网页中交互最多的形式之一,它通过各种形式接收用户的数据,包括下拉列表框,单选按钮,复选框和文本框,本篇主要介绍表单中常用的属性和方法 javascript中 ...

  4. Xdebug开源PHP程序调试器

    Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. 本文为大家讲解的是在linux下xdebug的安装和配置方法,感兴趣的同学参考下 ...

  5. CSS重点巩固

    一:position定位 a: static 定位 ,HTML元素的默认值,即没有定位,元素出现在正常的流中.静态定位的元素不会受到top, bottom, left, right影响. b: fix ...

  6. struts1日期转换处理

    问题场景 最近在维护公司旧的系统(用的struts1框架)的时候,在日期处理的时候,我将日期设定为Date类型,结果报以下错误: javax.servlet.ServletException: Bea ...

  7. 设计模式之UML类图的常见关系(一)

    本篇会讲解在UML类图中,常见几种关系: 泛化(Generalization),依赖(Dependency),关联(Association),聚合(Aggregation),组合(Compositio ...

  8. Cellphone Typing 字典树

    Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB   This problem will be judged on UVA. Ori ...

  9. tarjan算法--求无向图的割点和桥

    一.基本概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中 ...

  10. 【Matplotlib】 增加图例

    相关文档: Legend guide legend() command Legend API 控制图例入口 无参调用 legend() 会自动获取图例 handles 以及相关的 labels.其对应 ...