51Nod_1596 搬货物
http://www.51nod.com/Challenge/Problem.html#!#problemId=1596
题目
现在有n个货物,第i个货物的重量是 2wi 。每次搬的时候要求货物重量的总和是一个2的幂。问最少要搬几次能把所有的货物搬完。样例解释:1,1,2作为一组。3,3作为一组。
输入
单组测试数据。第一行有一个整数n (1≤n≤10^6),表示有几个货物。第二行有n个整数 w1,w2,...,wn,(0≤wi≤10^6)。
输出
输出最少的运货次数。
样例输入
5
1 1 2 3 3
样例输出
2
分析
每个货物都是2的幂次,要求搬的货物的总和也是2的幂次,因此如果是搬两个货物,那么它们的重量相等
C++程序
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
//输入加速
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x;
}
int main()
{
int n,w;
priority_queue<int,vector<int>,greater<int> >que;//值小优先
cin>>n;
for(int i=0;i<n;i++){
w=read();
que.push(w);
}
int ans=1;
w=que.top();
que.pop();
while(!que.empty()){
if(w<que.top()){
ans++;
w=que.top();
que.pop();
}
else if(w==que.top()){//如果重量相等可以一起搬
que.pop();
que.push(++w);
w=que.top();
que.pop();
}
}
cout<<ans<<endl;
return 0;
}