[Atcoder Code Festival 2017 Qual A Problem F] Squeezing Slimes

原题链接:https://code-festival-2017-quala.contest.atcoder.jp/tasks/code_festival_2017_quala_f

Squeezing Slimes

Problem Statement

There are A slimes lining up in a row. Initially, the sizes of the slimes are all 1.

Snuke can repeatedly perform the following operation.

Choose a positive even number M. Then, select M consecutive slimes and form M⁄2 pairs from those slimes as follows: pair the 1-st and 2-nd of them from the left, the 3-rd and 4-th of them, …, the (M−1)-th and M-th of them. Combine each pair of slimes into one larger slime. Here, the size of a combined slime is the sum of the individual slimes before combination. The order of the M⁄2 combined slimes remain the same as the M⁄2 pairs of slimes before combination.

Snuke wants to get to the situation where there are exactly N slimes, and the size of the i-th (1≤i≤N) slime from the left is ai. Find the minimum number of operations required to achieve his goal.

Note that A is not directly given as input. Assume A=a1+a2+…+aN.

Constraints

1≤N≤105
ai is an integer.
1≤ai≤ 109 10 9

Input

Input is given from Standard Input in the following format:

N
a1 a2 … aN

Output

Print the minimum number of operations required to achieve Snuke’s goal.

Sample Input 1

2
3 3

Sample Output 1

2

One way to achieve Snuke’s goal is as follows. Here, the selected slimes are marked in bold.

(1,1,1,1,1,1)(1,2,2,1) ( 1 , 1 , 1 , 1 , 1 , 1 ) → ( 1 , 2 , 2 , 1 )
(1,2,2,1)(3,3) ( 1 , 2 , 2 , 1 ) → ( 3 , 3 )

Sample Input 2

4
2 1 2 2

Sample Output 2

2

One way to achieve Snuke’s goal is as follows.

(1,1,1,1,1,1,1)(2,1,1,1,1,1) ( 1 , 1 , 1 , 1 , 1 , 1 , 1 ) → ( 2 , 1 , 1 , 1 , 1 , 1 )
(2,1,1,1,1,1)(2,1,2,2) ( 2 , 1 , 1 , 1 , 1 , 1 ) → ( 2 , 1 , 2 , 2 )

Sample Input 3

1
1

Sample Output 3

0

Sample Input 4

10
3 1 4 1 5 9 2 6 5 3

Sample Output 4

10

题目大意

n n 1组成的数列,每次可以选择连续偶数个数字。将第 1 1 个和第2个合并(求和),第 3 3 个和第4个合并,以此类推。要求最终结果是输入的数列。

至少要多少次操作能达到目标序列。

题解

毕克讲的全都是短代码神题 orz o r z

我们知道结束状态的数列,所以考虑倒推,每次选一些数分成两份,那么对于一个数 x x ,最快的拆分就是按二进制拆分,我们需要找到最大的mi使得 2mix 2 m i ≤ x

如果 2mi=x 2 m i = x ,就需要 mi m i 次操作将 x x 拆分;如果2mi<x,就需要再来一次。

考虑 x x 的前一个元素的拆分次数为now,当 now=mi n o w = m i 时,直接将上一个元素的操作区间扩大将 x x 覆盖就行了;如果now<mi,再扩大上一个元素的操作区间时,我们还需要更多的操作划分 x x now<mi时不再新增操作。

代码
#include<bits/stdc++.h>
using namespace std;
int n,x,mi,ans,dlt,now;
void in(){scanf("%d",&n);}
void ac()
{
    for(int i=1;i<=n;++i)
    {
        dlt=0;
        scanf("%d",&x);
        mi=(int)log2(x);
        if(x!=(1<<mi))dlt=1;
        if(now<mi)ans+=mi-now,now=mi;
        if(now>mi)now=mi,dlt=0;
        if(dlt)++ans,++now;
    }
    printf("%d",ans);
}
int main(){in();ac();}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值