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 ...
随机推荐
- Apache Tomcat8必备知识
Apache Tomcat8必备知识 作者:chszs,转载需注明.博客主页: http://blog.csdn.net/chszs 一.Apache Tomcat 8介绍 Apache Tomcat ...
- CF380C Sereja and Brackets [想法+线段树]
题意: 给出一串括号 给出一些询问,问某个区间[l,r]内的能合法匹配的括号数有多少个 分析: 我们能够实现处理两个数组 sum[i] 1....i中已经能匹配的右括号的数目 left[i] 1... ...
- Atitit.Gui控件and面板----数据库区-mssql 2008 权限 配置 报表查看成员
Atitit.Gui控件and面板----数据库区-mssql 2008 权限 配置 报表查看成员 1. 配置server连接权限 1 2. 配置数据库权限 1 3. 设置表格/视图安全性 2 1. ...
- [转] splice系列系统调用
关注splice系列系统调用(包括splice,tee和vmsplice)已经有一段时间了,开始的时候并未能领会splice的意义所在,致使得出了“splice系列系统调用不怎么实用”的错误结论.随着 ...
- 使用JDK自带缓存(Cache)实现Cookie自动登陆
自定义一个缓存类AdminCache package jw.admin.common; import jw.base.entity.Admin; import sun.security.util.Ca ...
- NYOJ-104最大和
我看了好多博客,都是拿一维的做基础,一维的比较简单,所以要把二维的化成一维的,一维的题目大意:给了一个序列,求那个子序列的和最大,这时候就可以用dp来做,首先dp[i]表示第i个数能构成的最大子序列和 ...
- C++输出hello world 详细注释
/* #include<iostream> #:预处理标志,后面跟预处理指令,include:预处理指令,包含 <iostream>:c++头文件,输入输出流 这行的作用就是在 ...
- Linux LVM 扩展磁盘分区
系统:centos 6.3--新建分区 fdisk -l /dev/sdc # 查看分区 fdisk /dev/sdc # 创建分区 :n ...
- ref传参时出错
下面是一段正确的代码: //基类A public class A { public void SomeMethod(A a) { } } //派生类B public class B:A { } //在 ...
- SQL Server 数据库邮件 配置
在 Sqlserver 上尝试配置 数据库邮件,用 smtp.qq.com 来发送邮件 如下配置后,发送测试邮件遇到错误 “由于邮件服务器故障,无法将邮件发送给收件人. (使用帐户 1 (2015-1 ...