UVA 1149 Bin Packing
2016-07-23 16:09
344 查看
UVA-1149
题意:给出n个物品和箱子长度 l,每个箱子最多装2个,求要用几个箱子装完这些东西。解题思路:先排序,然后从大的一边取,取到不能取了,换小的一边取取。注意控制次数,一共就取2个。
/************************************************************************* > File Name: 0722d.cpp > Author: Narsh > > Created Time: 2016年07月22日 星期五 19时09分18秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int s[101000],n,T,l,num; bool cmp(int a,int b) { return a<b; } int main() { scanf("%d",&T); while (T--) { scanf("%d%d",&n,&l); num=0; for (int i = 1; i <= n; i++) scanf("%d",&s[i]); sort(s+1,s+1+n); int h=1,t=n; while (h<=t) { num++; int leave=l,times=0;; while (leave >= s[t] && h <= t && times < 2) { leave-=s[t]; times++; t--; } while (leave >= s[h] && h <= t && times < 2) { leave-=s[h]; times++; h++; } } printf("%d\n",num); if (T) printf("\n"); } }
相关文章推荐
- CodeForces 629B Far Relative’s Problem
- 解决关于指针的疑问
- PAT1007
- hibernate集合映射inverse和cascade详解
- hdoj 1754 I Hate It <线段树--模板题>
- C++ const函数返回值必须为const引用
- 制作API文档
- POJ-----3040贪心
- JAVA面向对象-----main方法详解
- JAVA面向对象-----main方法详解
- Java volatile
- IntelliJ Idea 常用快捷键列表
- Hibernate中cascade作用
- MySQL数据库安装与配置
- javascript实现 时钟实时显示及幻灯片自动播放
- vimdiff使用
- 004-字符串分隔
- 6. ZigZag Conversion
- hdu 5725(计数)
- MySQL数据库的简介