码迷,mamicode.com
首页 > 其他好文 > 详细

Duff and Weight Lifting - 587A

时间:2015-10-29 20:16:04      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:某个人训练举重,他每次可以举起来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;
}

 

Duff and Weight Lifting - 587A

标签:

原文地址:http://www.cnblogs.com/liuxin13/p/4921535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!