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 ...
随机推荐
- 改变和恢复view的方向
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI/2); self.navigationCon ...
- C++之static
一.静态全局变量和非静态全局变量 1. 隐藏作用 比较非静态全局变量和静态(static)全局变量: 对于多个文件的代码,非静态全局变量和函数都是全局可见的.举例如下: a.c中: #include& ...
- Java基础知识强化20:面向对象和面向过程的思想对比
面向对象与面向过程的区别 1. 与面向对象编程思想相比较的,往往是面向过程的编程思想,其实在我来理解,两者并不冲突,原因是面向对象的编程也必须使用面向过程的思维来实现具体的功能,所以我认为,两者的区 ...
- LSI MegaCl i命令使用1
MegaCli命令使用:cd /opt/MegaRAID/MegaCli/MegaCli -AdpAllInfo -aAll [显示所有适配器信息]MegaCli -LDInfo -Lall ...
- 多重背包之 HDU -1171Big Event in HDU &HDU -2191悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
这两道题都是多重背包的基础题,前面的安格题意是:给出每个物体的价值和物体的数量,如何分使得A,B所得价值最接近并且A的价值不能小于B,就类似于NYOJ上的那个邮票分你一半那个意思,只不过这里不是一个而 ...
- Jenkins api java 调用
String filepath = "E:\\config.xml"; HttpClient client = new DefaultHttpClient(); HttpPost ...
- nosqlunit开源框架
import com.lordofthejars.nosqlunit.annotation.UsingDataSet;import com.lordofthejars.nosqlunit.core.L ...
- CDN的全称是Content Delivery Network,即内容分发网络
CDN的全称是Content Delivery Network,即内容分发网络 http://baike.baidu.com/link?url=Wd-IGGgslfJemdpuT3Y0BUi88RPQ ...
- Android开发手记(8) ProgressDialog的使用
ProgressDialog,进度对话框.一般有两种,一种是圆形的进度条(ProgressDialog.STYLE_SPINNER),另一种是长条形的进度条(ProgressDialog.STYLE_ ...
- mouseover,mouseout和mouseenter,mouseleave
mouseover和mouseout 鼠标指针进入或者离开被选元素或其子元素,都会触发相应事件. 非IE浏览器支持该事件. mouseenter和mouseleave 只有在鼠标指针进入或者离开被选元 ...