Codeforces 509c Sums of Digits 贪心

本文探讨了如何从数位和序列反推出原始序列的问题,详细介绍了解题思路,并提供了通过C语言实现的代码示例。解题过程中涉及了数位操作和递增序列的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:http://codeforces.com/contest/509/problem/C


C. Sums of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Sample test(s)
input
3
1
2
3
output
1
2
3
input
3
3
2
1
output
3
11
100



解题思路:给你一个序列B(序列B为序列A的数位和) 问你求A


两种情况讨论  设a[i]-a[i-1]=num

num>0 可以直接加1 (如果该位已经为9 就换到下一位上加)

num<0 这里就比较麻烦了 考虑他需要从后面借出这个num的值。而且要加到前面。这样一加一减。前面加了1 后面还需要多减去1 

得到num-1这个数字。高位的值不变。低位的值把他再和第一种情况一样做就可以了。


AC代码


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x0f0f0f0f
using namespace std;

int b[405],len,n,a[305];
void solve(int num)
{
     for (int i=1;num;++i){
       while (b[i]<9 && num){
             b[i]++;
             num--;
       }
       if (i>len && !num) len=i;
     }
}
void print()
{
	int i;
	for(i=len;i>0;i--) printf("%d",b[i]);
 	printf("\n");
}

int main()
{
	int i,j,k,l,m;
	scanf("%d",&n);
	for(i=1;i<=n;++i) 
		scanf("%d",&a[i]);
	solve(a[1]);
	print();
	for(int i=2;i<=n;++i)
	{
	    int delta=a[i]-a[i-1];
	    if (delta>0)
		{
	        solve(delta);
	        print();
	    }
		else
		{
	        int k=1;
	        while(1)
			{
	  			if(k>len) len=k;
	     		if(b[k]<9 && delta>0)
				{
	   				++b[k];
	       			solve(delta-1);
	          		print(); 
	          		break;
	          	}
	          	delta+=b[k];
	          	b[k]=0;
	          	k++;
	        }
	    } 
  	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值