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 ...
随机推荐
- Asm 常用资源
masm:http://www.aogosoft.com/masm:http://www.asmirvine.comasm:http://asm.yeah.netwin32asm:http://win ...
- Oracle管理监控之Oracle数据库存储空间监控
1.监控表空间使用率 基表:dba_data_files.dba_free_space 脚本: select a.tablespace_name, round((a.maxbytes / 1024 / ...
- Python的Flask框架应用调用Redis队列数据的方法
转自:http://www.jb51.net/article/86021.htm 任务异步化 打开浏览器,输入地址,按下回车,打开了页面.于是一个HTTP请求(request)就由客户端发送到服务器, ...
- SpringCloud 进阶之Zuul(路由网关)
1. Zuul(路由网关) Zuul 包含了对请求的路由和过滤两个最主要的功能; 路由功能:负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础; 过滤功能:负责对请求的处理过程进行干 ...
- AOP切点表达式
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execu ...
- LeetCode——Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- innobackupex 还原和备份实例
InnoDB 和非 InnoDB 文件的备份都是通过拷贝文件来做的,但是实现的方式不同,前者是以page为粒度做的(xtrabackup),后者是 cp 或者 tar 命令(innobackupex) ...
- java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事务
一.版本限制 参看地址:http://www.mybatis.org/spring/ 二.使用入门 2.1.pom <dependency> <groupId>org.myba ...
- A Basic Example of Threads Synchronization in Python, python中的线程同步示例
http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/ We will ...
- 文字和图片居中的HTML代码怎么写?
HTML 代码 ,怎么将文本/ 图片居中?这是在W3Cschool的编程问答中前端♌蕾儿提出的问题.网友施主同西否给出了详细的解答. html文字居中和html图片居中方法代码,通过在html中实现文 ...