51nod 1596 搬货物
2018-03-10 21:46
218 查看
1596 搬货物
题目来源: CodeForces基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
收藏
关注现在有n个货物,第i个货物的重量是 。每次搬的时候要求货物重量的总和是一个2的幂。问最少要搬几次能把所有的货物搬完。样例解释:1,1,2作为一组。3,3作为一组。
Input
反正我是没看懂
看了题解 貌似是转化二进制(摔
点击打开链接
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <iomanip>
#include <cstdio>
using namespace std;
int a[2000005];
map<int ,int > d;
map<int ,int >tag;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
a[x]++;
}
int sum=0;
for(int i=0;i<1100000;i++)
{
if(a[i]>1)
{
a[i+1]+=a[i]/2;
a[i]%=2;
}
if(a[i]==1) sum++;
}
printf("%d\n",sum);
}
}
题目来源: CodeForces基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
收藏
关注现在有n个货物,第i个货物的重量是 。每次搬的时候要求货物重量的总和是一个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 3Output示例
样例输出1 2
反正我是没看懂
看了题解 貌似是转化二进制(摔
点击打开链接
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <iomanip>
#include <cstdio>
using namespace std;
int a[2000005];
map<int ,int > d;
map<int ,int >tag;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
a[x]++;
}
int sum=0;
for(int i=0;i<1100000;i++)
{
if(a[i]>1)
{
a[i+1]+=a[i]/2;
a[i]%=2;
}
if(a[i]==1) sum++;
}
printf("%d\n",sum);
}
}
相关文章推荐
- 51Nod 1596 搬货物
- 51Nod-1596 搬货物
- 51nod-1596 搬货物
- 51nod 1596 搬货物 思路题
- 51Nod 1596 搬货物 二进制
- 51NOD 1596 搬货物
- 51nod-1596 搬货物
- 51nod 1596 搬货物
- 51Nod 1596 搬货物
- 51nod 1596搬货物 解题报告
- 51nod 1596 搬货物 2的倍数的运算+优先队列处理
- 51Nod-1596-搬货物
- 51nod 1596 搬货物
- 51nod 1596 搬货物
- 51Nod 1596 搬货物
- 【51nod 1596 搬货物】+ dp
- 51Nod - 1596 搬货物
- 51Nod 1596 搬货物
- 51Nod 1596 搬货物
- 51nod 1596 搬货物