Atcoder Grand Contest 011E - Increasing Numbers

探讨如何找出最少数量的非递减整数,使这些整数的和等于给定整数N。通过巧妙转换问题,利用特殊形式的数p[i](如1, 11, 111等),将问题转化为一个更易处理的形式。

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

Problem Statement

We will call a non-negative integer increasing if, for any two adjacent digits in its decimal representation, the digit to the right is greater than or equal to the digit to the left. For example, 1558113 and 0 are all increasing; 10 and 20170312 are not.

Snuke has an integer N. Find the minimum number of increasing integers that can represent N as their sum.

Constraints

  • 1N10500000

Input

The input is given from Standard Input in the following format:

N

Output

Print the minimum number of increasing integers that can represent N as their sum.


Sample Input 1

Copy
80

Sample Output 1

Copy
2

One possible representation is 80=77+3.


Sample Input 2

Copy
123456789

Sample Output 2

Copy
1

123456789 in itself is increasing, and thus it can be represented as the sum of one increasing integer.


Sample Input 3

Copy
20170312

Sample Output 3

Copy
4

Sample Input 4

Copy
7204647845201772120166980358816078279571541735614841625060678056933503

Sample Output 4

Copy
31
题意:给一个N,求最少的数使得它们和为N,并且它们从高位到低位单调不降
题解:考虑这种数一定可以表示成9个p[i]的和
p[0]=0, p[1]=1, p[2]=11, p[3]=111...
假设答案为k,那么列出式子N=sum[i = 1 to 9k] p[x[i]](x[i]表示选出的数)
由于11111111不好看,我们乘上9
9N=sum[i = 1 to 9k] (10 ^ (x[i] + 1) - 1)->9N + 9k = sum[i = 1 to 9k] 10 ^ x[i]
这就做完了
好厉害
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 500050;

char s[MAXN];
int ans, l, r, n, a[MAXN];

inline bool chk(int x)
{
	int len = n, sum = 0;
	for( int i = 1 ; i <= n ; i++ ) a[ i ] = ( s[ i ] - '0' ) * 9;
	a[ 1 ] += 9 * x;
	for( int i = 1 ; i <= len ; i++ )
		if( a[ i ] > 9 )
		{
			if( i == len ) a[ ++len ] = 0;
			a[ i + 1 ] += a[ i ] / 10;
			a[ i ] %= 10;
		}
	for( int i = 1 ; i <= len ; i++ ) sum += a[ i ];
	return sum <= 9 * x;
}

int main()
{
#ifdef wxh010910
	freopen( "data.in", "r", stdin );
#endif
	scanf( "%s", s + 1 );
	l = 1, r = n = strlen( s + 1 );
	reverse( s + 1, s + n + 1 );
	while( l <= r )
	{
		int mid = l + r >> 1;
		if( chk( mid ) ) ans = mid, r = mid - 1;
		else l = mid + 1;
	}
	cout << ans << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值