51nod 1596 搬货物

1596 搬货物 

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

样例解释:

1,1,2作为一组。

3,3作为一组。

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

由于22+22=23d,所以,从1开始遍历到最大值,已经奇数的就加1,然后除以2往后面放。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdio.h>
 4 using namespace std;
 5 const int N = 1e6+100;
 6 int w[N];
 7 int main() {
 8     ios::sync_with_stdio(false);
 9     cin.tie(0);
10     cout.tie(0);
11     int n, x, ans = 0;
12     cin >> n;
13     for(int i = 0; i < n; i ++) {
14         cin >> x;
15         w[x]++;
16     }
17     for(int i = 0; i < N; i ++) {
18         if(w[i]) {
19             ans += w[i]%2;
20             w[i+1] += w[i]/2;
21         }
22     }
23     cout << ans << endl;
24     return 0;
25 }
原文地址:https://www.cnblogs.com/xingkongyihao/p/8893867.html