Duff and Weight Lifting - 587A

题目大意:某个人训练举重,他每次可以举起来2^wi的重量,不过这个人比较懒所以他想尽量减少训练的次数,如果所有的训练重量2^a1 +2^a2+....2^ak = 2^x,那么这些重量可以一次性训练(不需要连续),问他最少要训练几次才行。

 

分析:

已知 2^x+2^x = 2^(x+1),所以相同的是可以合并成下一个的,最后只需要判断,有多少个不能合成的即可。

代码如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;

const int MAXN = 2e6+7;

int a[MAXN+1];

int main()
{
    int N, x, ans=0;

    scanf("%d", &N);

    for(int i=0; i<N; i++)
    {
        scanf("%d", &x);
        a[x] += 1;
    }

    for(int i=0; i<MAXN; i++)
    {
        a[i+1] += a[i] / 2;
        a[i] %= 2;
        ans += a[i];
    }

    printf("%d\n", ans);

    return 0;
}

 

posted @ 2015-10-29 19:01  无忧望月  阅读(246)  评论(0编辑  收藏  举报
levels of contents