51NOD 1596 搬货物

1596 搬货物
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
收藏
关注

现在有n个货物,第i个货物的重量是 2wi 。每次搬的时候要求货物重量的总和是一个2的幂。问最少要搬几次能把所有的货物搬完。

样例解释:

112作为一组。

33作为一组。

Input

单组测试数据。
第一行有一个整数n (1≤n≤10^6),表示有几个货物。
第二行有n个整数 w1,w2,...,wn,(0≤wi≤10^6)。

Output

输出最少的运货次数。

Input示例

样例输入1
5
1 1 2 3 3

Output示例

样例输出1
2

#include<iostream>
#include<stdio.h>
#include <algorithm>
#include<deque>
#include<set>
using namespace std;
#define INF 1e+7

const int N = 1e6 + 5;
int num[2*N];

int main() {
    //freopen("d:\\DATA_IN.txt", "r", stdin);
    int n, w, ans = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &w);
        ++num[w];
    }
    for (int i = 0; i < 2 * N; ++i) {
        ans += num[i] & 1;
        num[i + 1] += num[i] >> 1;
    }
    printf("%d\n", ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值