UVA-1149 Bin Packing (贪心)
题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子。
题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最重的单独装到一个箱子里。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; int a[100005]; int solve(int n,int m)
{
int l=0,r=n-1,ans=0;
while(l<=r){
if(a[l]+a[r]<=m)
++l,--r;
else
--r;
++ans;
}
return ans;
} int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
scanf("%d",a+i);
sort(a,a+n); printf("%d\n",solve(n,m));
if(T)
printf("\n");
}
return 0;
}
UVA-1149 Bin Packing (贪心)的更多相关文章
- uva 1149:Bin Packing(贪心)
题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- UVA 1149 Bin Packing 装箱(贪心)
每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...
- UVA - 1149 Bin Packing(装箱)(贪心)
题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品.求至少要多少个背包才能装下所有的物品. 分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个 ...
- UVA 1149 Bin Packing
传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- UVa 102 - Ecological Bin Packing(规律,统计)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVa - 102 - Ecological Bin Packing
Background Bin packing, or the placement of objects of certain weights into different bins subject t ...
- Bin Packing
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...
随机推荐
- FZU 2098 刻苦的小芳(卡特兰数,动态规划)
Problem 2098 刻苦的小芳 Accept: 42 Submit: 70 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Descr ...
- 沈阳网络赛G-Spare Tire【容斥】
17.64% 1000ms 131072K A sequence of integer \lbrace a_n \rbrace{an} can be expressed as: \display ...
- python sort、sorted高级排序技巧(转)
add by zhj: 没找到原文.可以按多个维度进行排序,而且可以指定他们的排序方向,如果维度都是数字,排序比较容易,用+/-号就可以 指定排序方向.否则,就调用多次sorted进行排序了,而且要按 ...
- idea 设置
1.代码提示不区分大小写 2.自动导入 抽取成接口-push members up 你可能不知道的IDEA使用技巧
- eclipse导入Java源码
eclipse导入Java源码 下载源码包(一般jdk都自带了, 我的没有) src.zip eclipse -> window -> preferences -> JAVA -&g ...
- 自定义查询语句SpringData
虽然官方的API中给我们提供了很多关键字的查询,但是还是不够灵活,因为我们在项目中,会遇见奇葩的业务,我们需要用SpringData中的一个@Query注解. 使用@Query自定义查询 这种查询可以 ...
- NPOI+反射+自定义特性实现上传excel转List及验证
1.自定义特性 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public ...
- 142. Linked List Cycle II(找出链表相交的节点)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest Solution
A. Rikka with Minimum Spanning Trees 题意: 给出一个图,求最小生成树的个数和权值 思路: 因为数据随机,只有一个MST #include <bits/std ...
- 20155334 2016-2017-2 《Java程序设计》第七周学习总结
20155334 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章:Lambda 认识Lammbda语法 Lambda去可以重复,符合DRY原则,而且 ...