您的位置:首页 > 其它

AtCoder2341 - Increasing Numbers - 二分+思维

2017-05-24 17:55 471 查看
1.题目描述:


E - Increasing Numbers

Time limit : 2sec / Memory limit : 256MB

Score : 1300 points

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, 1558, 11, 3 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

1≤N≤10500000

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


2.题意概述:
当一个整数高位数字总不小于低位数字,或者说写成字符串之后单调不下降,称之为上升数。求一个整数最少能表示为多少个上升数的和。

3.解题思路:



4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 500100
#define lson root << 1#define rson root << 1 | 1#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
char s[maxn];
int a[maxn];
int len;
bool check(int x)
{
int sum = x * 9, cnt = 0;
for (int i = 0; i < len; i++)
{
sum += a[i];
cnt += sum % 10;
sum /= 10;
}
while (sum > 0)
{
cnt += sum % 10;
sum /= 10;
}
return cnt <= x * 9;
}
int main()
{
while (~scanf("%s", s))
{
memset(a, 0, sizeof(a));
len = strlen(s);
reverse(s, s + len);
for (int i = 0; i < len; i++)
a[i] = (s[i] - '0') * 9;
for (int i = 0; i < len; i++)
{
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
if (a[len])
len++;
int l = 0, r = len * 2, ans = -1;
while (l < r)
{
int mid = l + r >> 1;
if (check(mid))
{
ans = mid;
r = mid;
}
else
l = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm AtCoder