搬货物

来源:互联网 发布:汽车内饰件图解知乎 编辑:程序博客网 时间:2024/06/10 08:18

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

样例解释:

112作为一组。

33作为一组。


Input
单组测试数据。第一行有一个整数n (1≤n≤10^6),表示有几个货物。第二行有n个整数 w1,w2,...,wn,(0≤wi≤10^6)。
Output
输出最少的运货次数。
Input示例
样例输入151 1 2 3 3
Output示例
样例输出12

#include <stdio.h>#include <string.h>#include <math.h>const int SIZE = 1e6 + 50;int buf[SIZE];int main(int argc, const char * argv[]){int n;scanf("%d", &n);memset(buf, 0, sizeof(buf));    int a;for (int i = 0; i < n; i++){scanf("%d", &a);buf[a]++;}int result = 0;for (int i = 0; i < SIZE; i++){buf[i+1] += (buf[i] >> 1);result += (buf[i]&1);}printf("%d\n", result);return 0;}