51Nod 1596 搬货物 (数学

来源:互联网 发布:淘宝直播流量来源 编辑:程序博客网 时间:2024/05/19 23:24

1596 搬货物

题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注
现在有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

很有意思的一道题 (利用二进制2333

#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <iostream>#include <vector>#include <algorithm>using namespace std;#define ll long longconst int mod = 1e9+7;#define N 2000010int arr[N]; int main(){    int n;    while(~scanf("%d",&n)) {        int k;        memset(arr,0,sizeof(arr));        for(int i = 0;i < n; i++) {            scanf("%d",&k);            arr[k]++;        }        int sum = 0;        for(int i = 0;i < N; i++) {            if(arr[i] > 1) {                arr[i+1] += arr[i]/2;                arr[i] %= 2;            }            if(arr[i] == 1) sum++;        }        printf("%d\n",sum);    }return 0;}
1 0
原创粉丝点击