原题链接: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≤
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.
Sample Input 2
4
2 1 2 2
Sample Output 2
2
One way to achieve Snuke’s goal is as follows.
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
题目大意
有 个 组成的数列,每次可以选择连续偶数个数字。将第 个和第 个合并(求和),第 个和第 个合并,以此类推。要求最终结果是输入的数列。
至少要多少次操作能达到目标序列。
题解
毕克讲的全都是短代码神题 。
我们知道结束状态的数列,所以考虑倒推,每次选一些数分成两份,那么对于一个数 ,最快的拆分就是按二进制拆分,我们需要找到最大的 使得 。
如果 ,就需要 次操作将 拆分;如果 ,就需要再来一次。
考虑 的前一个元素的拆分次数为 ,当 时,直接将上一个元素的操作区间扩大将 覆盖就行了;如果 ,再扩大上一个元素的操作区间时,我们还需要更多的操作划分 ; 时不再新增操作。
代码
#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();}