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 ...
随机推荐
- os即时通讯客户端开发之-mac上安装MySQL
一.安装 到MySQL官网上http://dev.mysql.com/downloads/mysql/,下载mysql可安装dmg版本 比如:Mac OS X ver. 10.7 (x86, 64-b ...
- Android中自定义Activity和Dialog的位置大小背景和透明度等
1.自定义Activity显示样式 先在res/values下建colors.xml文件,写入: view plainprint? 1. <?xml version="1.0" ...
- [Flexbox] Using flex-direction to layout content horizontally and vertically
The Flexbox css spec allows for more adjustable layouts. The flex-directionproperty allows you to ea ...
- 查看linux版本号的几种方法
(1)lsb_release 命令查看,FSG(Free Standards Group)组织开发的LSB (Linux Standard Base)标准的一个命令,用来查看linux兼容性的发行版信 ...
- 排序算法源码(JAVA)
public class Test { public static void main(String[] args){ Sort sort = new Sort(); sort.sortTest( ...
- ExecuteNonQuary接收存储过程的输出类型的变量的值
1.设置所调用的存储过程需要的参数 public decimal CreateOrder(string orderId, int userId, string address) { SqlParame ...
- Asp.net笔记(1)
1.下拉框,列表,下拉列表 下拉框其实是HTML的知识,在这里就是在复习一下: <select id="select1" runat="server"&g ...
- 关于<%@ include file=" " %>与<jsp:include page=""></jsp:include>中的那些问题?
今天在使用<%@ include file=" " %>指令时,竟然在页面中不让使用?这是怎么回事:问题如下图: 顿时被这个问题给搞到了!!!突然想到在以前的 JSP ...
- 用javascript预加载图片、css、js的方法研究
预加载的好处可以让网页更快的呈现给用户,缺点就是可能会增加无用的请求(但图片.css.js这些静态文件可以被缓存),如果用户访问的页面里面的css.js.图片被预加载了,用户打开页面的速度会快很多,提 ...
- Redis同步(主从复制)
目录1.Replication的工作原理2.如何配置Redis主从复制3.应用示例 1.Replication的工作原理在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Ma ...