Codeforces 509C Sums of Digits

时间:2024-04-26 23:58:20

http://codeforces.com/contest/509/problem/C

 题目大意:

给出一个序列,代表原序列对应位置数的每一位的数字之和,原序列单调递增,问原序列的最后一个数最小的方案每一个数是多少。

思路:贪心,从1到n,我们尽量让每个数最小就可以了。

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int g[],n,a[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void work(int x,int y){
if (x==y){
g[]++;
int i=;
while (i<=g[]||g[i]>){
g[i+]+=g[i]/;
g[i]%=;
i++;
}
while (i>&&g[i]==) i--;
g[]=i;
x=;
for (int i=;i<=g[];i++)
x+=g[i];
}
if (x>y){
int j=,i;
for (i=;i<=g[]&&x-j>=y;i++) j+=g[i];
while (g[i]==) i++;
for(g[i--]++;i;i--) g[i]=;
int k=;
while (k<=g[]||g[k]>){
g[k+]+=g[k]/;
g[k]%=;
k++;
}
while (k>&&g[k]==) k--;
g[]=k;
x=;
for (int i=;i<=g[];i++)
x+=g[i];
}
int i;
for (i=;x<y;i++)
if (-g[i]+x>=y) g[i]+=y-x,x=y;
else x+=-g[i],g[i]=;
if (g[]<i-) g[]=i-;
}
int main(){
n=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=n;i++){
work(a[i-],a[i]);
for (int j=g[];j>=;j--)
printf("%d",g[j]);
puts("");
}
return ;
}