【Codeforces Round 326 (Div 2)C】【贪心】Duff and Weight Lifting 每次取数二的幂数最小取数次数

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 of i-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 integer x 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.

Sample test(s)
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.


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e6+30,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n,x;
int a[N];
int main()
{
	while(~scanf("%d",&n))
	{
		MS(a,0);
		int top=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&x);
			++a[x];
			gmax(top,x);
		}
		int ans=0;
		for(int i=0;i<=top+19;i++)
		{
			a[i+1]+=a[i]>>1;
			ans+=(a[i]&1);
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
我们检查合并,最多是会合并的到多19层。
假设top=0,有1e6个2^0,那么就会最终合并到2^19

【题意】
给你n(1<=n<=1e6)个数a[],(0<=a[]<=1e6)。
a[i]表示这个数实际上是2^a[i]。
我们每次取数,可以还没有取的数中任取一些数,
要求这些数的和必须恰好是2^p(0<=p<=inf)。
问你至少要取多少次可以把所有数取完。

【类型】
贪心

【分析】
因为取数涉及到数的合并,数越合并就会变得越大。
所以我们不妨——用从小到大的顺序,进行思考问题。
对于一个现有的2^k,个数a[k]为num个,如果num为奇数,那这些数就一定要单独取一次。
否则我们一定合并,为什么呢?因为不合并的话,还要再取一次,而且对后面的决策并没有增益。
按照这个原则,一直贪下去,就可以AC啦。

【时间复杂度&&优化】
O(n)

*/


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值