Codeforces 588 C Duff and Weight Lifting【思维】

来源:互联网 发布:手机游戏下载java 编辑:程序博客网 时间:2024/05/01 13:35

C. Duff and Weight Lifting

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight ofi-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.

Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integerx such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.

Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.

Input

The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights.

The second line contains n integers w1, ..., wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.

Output

Print the minimum number of steps in a single line.

Examples

Input

5
1 1 2 3 3

Output

2

Input

4
0 1 2 3

Output

4

Note

In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.

In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.

 

题目大意:有n个数,每个数都表示2^a【i】的数,每一次选取可以从n个数中挑取任意个数的数字,必须让其和是2的幂数才行,问最少选取几次才能将所有数都取走。


思路:


1、首先明确这一点,在二进制中,想要进位一定是同位相加才能进位。那么给出的数又是2的幂数,那么明显只有a【i】==a【j】的时候,才有进位合并的可能。


2、那么设定一个数组vis【i】表示2^i有多少个。那么我们从0开始扫,令vis【i】=vis【i】%2;vis【i+1】=vis【i】/2,即可,最后ans=累加vis【i】;


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int vis[1000500];int main(){    int n;    while(~scanf("%d",&n))    {        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            int x;            scanf("%d",&x);            vis[x]++;        }        for(int i=0;i<1000400;i++)        {            vis[i+1]+=vis[i]/2;            vis[i]%=2;        }        int output=0;        for(int i=0;i<1000400;i++)        {            output+=vis[i];        }        printf("%d\n",output);    }}




0 0