cf509C 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.
3
1
2
3
1
2
3
3
3
2
1
3
11
100 题意是给你一个递增序列的每个数的各位数字之和,求还原这个数列
显然贪心,每次用比上一个数大的最小的那个就行了
但是这题模拟实现太蛋疼了
而且极限数据是一开始a[1]=300要凑出3后面33个9,然后a[2]到a[300]是299个1……这样只开300零几位的就死了
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7fffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,len;
int a[500],s[500],t[500];
inline void calc(int x,int l)
{
memset(s,0,sizeof(s));
len=0;x--;s[l]=1;
while (x>=9&&len<l)
{
s[++len]=9;
x-=9;
}
if (x)s[++len]+=x;
len=l;
}
inline bool getmn(int x)
{
t[len+1]=0;
for (int i=len;i>=1;i--)
t[i]=t[i+1]+s[i];
for (int i=1;i<=len;i++)
{
if (t[i]+1>x||s[i]==9)continue;
s[i]++;
while (x>9*(i-1)+t[i+1]+s[i]&&s[i]<9)s[i]++;
if (x>9*(i-1)+t[i+1]+s[i])continue;
x=x-(t[i+1]+s[i]);
int now=1;
while(now<i&&x>=9)
{
s[now++]=9;
x-=9;
}
if (now!=i)s[now++]=x;
for (int j=now;j<i;j++)s[j]=0;
return 1;
}
return 0;
}
inline void put()
{
for (int i=len;i>=1;i--)
printf("%d",s[i]);
printf("\n");
}
int main()
{
n=read();
for (int i=1;i<=n;i++)a[i]=read();
calc(a[1],a[1]/9+(a[1]%9!=0));
put(); for (int i=2;i<=n;i++)
{
if (a[i]>9*len)calc(a[i],a[i]/9+(a[i]%9!=0));
if (!getmn(a[i]))calc(a[i],max(a[i]/9+(a[i]%9!=0),len+1));
put();
}
}
cf509C Sums of Digits的更多相关文章
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- 【codeforces 509C】Sums of Digits
[题目链接]:http://codeforces.com/contest/509/problem/C [题意] 给你一个数组b[i] 要求一个严格升序的数组a[i]; 使得a[i]是b[i]各个位上的 ...
- [模拟]Codeforces509C Sums of Digits
题目链接 题意:给n个数a[i], 要求b[i]每位数的和等于a[i], 并且b[i]要严格递增 求最小的b[i] b[0]最小一定是X9999...这样的形式 后面的b[i]位数一定大于等于前一个 ...
- 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> # ...
- AtCoder Beginner Contest 083 (AB)
A - Libra 题目链接:https://abc083.contest.atcoder.jp/tasks/abc083_a Time limit : 2sec / Memory limit : 2 ...
- codeforce - 13A A.Numbers
A. Numbers time limit per test 1 second memory limit per test 64 megabytes input standard input outp ...
随机推荐
- 外键约束列并没有导致大量建筑指数library cache pin/library cache lock
外键约束列并没有导致大量建筑指数library cache pin/library cache lock 清除一个100大数据表超过一百万线,发现已经运行了几个小时: delete B001.T_B1 ...
- Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- Java基础知识强化91:DateFormat类之DateFormat实现日期和字符串的相互转换
1. DateFormat类概述: DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFormat 2. ...
- HDU -2546饭卡(01背包+贪心)
这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...
- Swift - 35 - 使用闭包简化语法
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...
- php 设置报错等级
定义和用法: error_reporting() 设置 PHP 的报错级别并返回当前级别. 函数语法: error_reporting(report_level) 如果参数 level 未 ...
- 分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var)
BOOL型变量:if(!var) int型变量: if(var==0) float型变量: const float EPSINON = 0.00001; if ((x > ...
- 关于webApi302跳转的问题
之前会出现"服务器无法在已发送 HTTP 标头之后设置状态"的问题,本地调试不报错,但是上产线就会报错 解决的思路是: var response = Request.CreateR ...
- 带格式导出数据到Excel
导出按钮事件 protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); ...
- nodejs学习记录
一.环境搭建 1.安装express 局部安装 npm i express 全局安装 npm i -g express (命令行express无法使用) 环境变量 npm i -g express-g ...