您的位置:首页 > 其它

51nod 1596 搬货物

2017-05-11 14:54 351 查看
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


暴力乱搞一发过了,时间还贼快。。。

#include <iostream>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1100000;
ll b[2]
;

const int MAXS=10*1024*1024;
char buf[MAXS],*ch;
void read(ll &x)
{
while(*(++ch)<'0');
for(x=0;*ch>='0';++ch) x=x*10+*ch-'0';
}
void read_init()
{
ch=buf-1;
fread(buf,1,MAXS,stdin);
}

int main()
{
ll n;
int t;
read_init();
read(n);
for(int i=0;i<(int)n;i++) read(b[0][i]);
int n1=(ll)n,n2=0;
t=0;
while(1)
{
if(n1==n2) break;
sort(b[t],b[t]+n1);
n2=0;
for(int i=0;i<n1;i++)
{
if(i<n1-1&&b[t][i]==b[t][i+1])
{
b[t^1][n2++]=b[t][i]+1;
i++;
}
else
{
b[t^1][n2++]=b[t][i];
}
}
t^=1;
swap(n1,n2);
}
cout<<n1<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: