高效算法——Bin Packing F - 贪心
Description
A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l<tex2html_verbatim_mark> and each item i<tex2html_verbatim_mark> has length lil<tex2html_verbatim_mark> . We look for a minimal number of bins q<tex2html_verbatim_mark> such that
- each bin contains at most 2 items,
 - each item is packed in one of the q<tex2html_verbatim_mark> bins,
 - the sum of the lengths of the items packed in a bin does not exceed l<tex2html_verbatim_mark> .
 
You are requested, given the integer values n<tex2html_verbatim_mark> , l<tex2html_verbatim_mark> , l1<tex2html_verbatim_mark> , ..., ln<tex2html_verbatim_mark> , to compute the optimal number of bins q<tex2html_verbatim_mark> .
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of the input file contains the number of items n<tex2html_verbatim_mark>(1n
105)<tex2html_verbatim_mark> . The second line contains one integer that corresponds to the bin length l
10000<tex2html_verbatim_mark> . We then have n<tex2html_verbatim_mark> lines containing one integer value that represents the length of the items.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For each input file, your program has to write the minimal number of bins required to pack all items.
Sample Input
1 10
80
70
15
30
35
10
80
20
35
10
30
Sample Output
6
Note: The sample instance and an optimal solution is shown in the figure below. Items are numbered from 1 to 10 according to the input order.
#include <cstdio>
#include <algorithm>
using namespace std;
int a[];
int n,l;
int init()
{
sort(a,a+n);
int i,c=, j=;
for(i=n-;i>=;i--)
{
if(i<j) break;
c++;
while(a[i]+a[j]<=l&&i>j)
{
j++;
break;
}
}
return c;
}
int main()
{
int t,f=;
scanf("%d",&t);
while(t--)
{
if(f>) printf("%d\n");
f++;
scanf("%d%d",&n,&l);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
printf("%d\n",init());
} return ; }
高效算法——Bin Packing F - 贪心的更多相关文章
- 集训第四周(高效算法设计)F题 (二分+贪心)
		
Description A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...
 - 高效算法——Most financial institutions  贪心 H
		
H - 贪心 Crawling in process... Crawling failed Time Limit:3000MS Memory Limit:0KB 64bit IO Fo ...
 - UVA-1149 Bin Packing (贪心)
		
题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最 ...
 - 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 装箱(贪心)
		
每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...
 - poj 2782 Bin Packing (贪心+二分)
		
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
 - Bin Packing
		
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...
 - python常用算法(6)——贪心算法,欧几里得算法
		
1,贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的的时在某种意义上的局部最优解. 贪心算法并不保证会得到最优解,但 ...
 
随机推荐
- (转)从内存管 理、内存泄漏、内存回收探讨C++内存管理
			
http://www.cr173.com/html/18898_all.html 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟 ...
 - WPF RichTextBox 如何滚动到光标所在位置、滚动条操作
			
1.获取当前滚动条位置 //获取当前滚动条位置 richTextBox.VerticalOffset; richTextBox.HorizontalOffset; //获取当前光标位置 richTex ...
 - Android之如何混淆代码和相关配置
			
昨天,客户想看一下目前项目开发到什么程度了,于是需要将项目签名打包成apk,结果打包的时候出错了,吃惊,什么情况.等成功打包以后,安装起来发现部分功能又报错了,囧,所幸最后还是解决了.在这里记录一下遇 ...
 - "库未注册"(Library not registered)异常.
			
启发链接:http://social.msdn.microsoft.com/Forums/vstudio/en-US/f25b80bc-ecd4-4c37-8be3-9106a765b072/libr ...
 - UITableViewCell 上的按钮点击事件
			
以前做tableViewCell的button点击事件,总是建立一个全局的可变数组,把data放在数组里,点击获取button的tag值,根据tag从数组了里取data. 其实,如果section只 ...
 - VB热点答疑(2016.5.11更新Q4、Q5)
			
收录助教君在VB习题课上最常被问到的问题,每周更新,希望对大家有所帮助. Q1.如何让新的文本内容接在原来的内容后面/下一行显示? A1.例如,Label1.text原本的内容是"VB程序设 ...
 - printf 缓冲区问题
			
突然发现printf的问题,看了这个很有意思,学习一下 转自:http://blog.csdn.net/shanshanpt/article/details/7385649 昨天在做Linux实验的时 ...
 - 仿照淘宝首页做的一个高度伪对齐demo
			
功能就是当右边高度没有左边高的情况下做的一些处理,由于本人技术有限,不兼容所有浏览器, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...
 - maven 常用插件总结
			
maven-javadoc-plugin (1) 说明:该插件生成项目的javadoc.对于构建jar目标,javadoc会首先生成并打包放入jar文件中. (2) 默认用法: pom.xml配置 & ...
 - seaJs初体验
			
目录结构 模块定义define define(function(require,exports,module){ //exports可以把方法或属性暴露给外部 exports.name = 'hell ...