[codeforces 509]C. Sums of Digits
[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的更多相关文章
- 【codeforces 509C】Sums of Digits
[题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...
- cf509C Sums of Digits
C. Sums of Digits time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- Codeforces 509C Sums of Digits
http://codeforces.com/contest/509/problem/C 题目大意: 给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方 ...
- Codeforces 509C Sums of Digits 贪心
这道题目有人用DFS.有人用DP 我觉得还是最简单的贪心解决也是不错的选择. Ok,不废话了,这道题目的意思就是 原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Arra ...
- CodeForces 509C Sums of Digits(贪心乱搞)题解
题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...
- Sums of Digits CodeForces - 509C (贪心,模拟)
大意: 一个未知严格递增数组$a$, 给定每个数的数位和, 求$a[n]$最小的数组$a$ #include <iostream> #include <algorithm> # ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 509 D. Restoring Numbers(数学+构造)
题目链接:http://codeforces.com/problemset/problem/509/D 题意:题目给出公式w[i][j]= (a[i] + b[j])% k; 给出w,要求是否存在这样 ...
随机推荐
- ThinkPHP中简单的CURD操作
前言 我们通过一个简答例子来简述CURD的操作.首先看一下数据库的样子,其中id为自增行,其它是varchar 一.查询操作 首先,创建在Controller文件夹下创建一个User控制器,在该控制器 ...
- JS面向对象高级特性
本篇是通过学习视频<一头扎进javascirpt高级篇>整理的一些相关知识,大致包括下面几个方面: 1 对象的创建方法 2 对象的对象属性.私有属性.类属性 3 对象的对象方法.私有方法. ...
- iOS彩票项目--第七天,初次读取json数据、KVC转模型技巧、运行时字典转模型以及初步对显示网页的操作并且跟踪标签
一.初次读取json数据 二.KVC转模型技巧,这里的技巧主要解决的是字典中的key 与 模型中有的属性对应不起来的时候 的解决办法 <方法1> <方法2>运行时字典转模型,运 ...
- 在java类中,是先执行类的构造函数还是先执行类的私有非静态变量
举例子: package test_instance; public class TestClassLoaderTime { public TestClassLoaderTime(){ System. ...
- 【前端】Sublime text3 插件HTML/CSS/JS prettify 格式化代码
1.首先安装插件 菜单的preference->packages control,然后输入install .. 回车,再输入HTML/CSS/JS prettify 再回车,重启后就可以了. 2 ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- 【BZOJ-1588】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- ubuntu设置屏幕亮度
楼主的本本是acer4750g ,系统是ubuntu kylin 14.04 原 笔记本ubuntu14.04无法调节屏幕亮度 http://my.oschina.net/lhplj/blog/397 ...
- 点击div区域以外部分,div区域隐藏
核心思想: 监听body的click事件,事件触发的时候判断是否发生在弹出的div上,如果不在,关闭弹层 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...
- hdu 1575 矩阵快速幂模板
#include "iostream" #include "vector" #include "cstring" using namespa ...