51nod 1596 搬货物

  对于这n堆货物,能进行合并就进行合并,合并到不能合并为止,然后要算一下2的幂有多少个即可,合并的话是只有2个2的同次幂可以合并,2的不同次幂不可以合并。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+30;
int cnt[maxn];
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,ans=0;
    n=read();
    for(int i=1;i<=n;i++)
    {
        int tmp;
        tmp=read();
        cnt[tmp]++;
    }
    for(int i=0;i<=maxn;i++)
    {
        cnt[i+1]+=cnt[i]/2;
        cnt[i]=cnt[i]%2;
        ans+=cnt[i];
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/eason9906/p/11754857.html