CF279E.Beautiful Decomposition

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Valera considers a number beautiful, if it equals 2_k_ or -2_k_ for some integer k (k ≥ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible.

Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands.

瓦列拉认为一个数是“美丽的”,当且仅当它等于 2k2^k 或 −2k-2^k(其中 kk 为某个整数,且 k≥0k \geq 0)。最近,数学老师要求瓦列拉将整数 nn 表示为若干个美丽数字之和。由于瓦列拉非常贪心,他希望使用尽可能少的美丽数字来完成该任务。

请帮助瓦列拉求出他最少需要多少个美丽数字。换言之,在所有将 nn 分解为美丽数字之和的方式中,你需要找出加数个数最少的那种分解方式所含加数的个数。

输入格式

The first line contains string s (1 ≤ |s| ≤ 106), that is the binary representation of number n without leading zeroes (n > 0).

第一行包含字符串 ss(1 ≤ ∣s∣ ≤ 1061 ≤ |s| ≤ 10^6),即正整数 nn(n > 0n > 0)的二进制表示,不含前导零。

输出格式

Print a single integer — the minimum amount of beautiful numbers that give a total of n.

输出一个整数——组成总数 nn 所需的优美数的最少个数。

输入输出样例

  • 输入#1

    10

    输出#1

    1
  • 输入#2

    111

    输出#2

    2
  • 输入#3

    1101101

    输出#3

    4

说明/提示

In the first sample n = 2 is a beautiful number.

In the second sample n = 7 and Valera can decompose it into sum 23 + ( - 20).

In the third sample n = 109 can be decomposed into the sum of four summands as follows: 27 + ( - 24) + ( - 22) + 20.

在第一个样例中,n=2n = 2 是一个“优美数”。

在第二个样例中,n=7n = 7,Valera 可将其分解为和式 23+(−20)2^3 + (-20)。

在第三个样例中,n=109n = 109 可被分解为如下四个加数的和:27+(−24)+(−22)+202^7 + (-2^4) + (-2^2) + 2^0。

输入解题思路,AI测评打分。不知道怎么写?

首页