Bin Packing

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F

题目:

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>(1n105)<tex2html_verbatim_mark> . The second line contains one integer that corresponds to the bin length l10000<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.

 

题意:

给定n个物品的长度a[i],每个箱子的长度是l,同时要求每个箱子最多装两个物品。求至少要多少个箱子才能装下所有的物品。

分析:

将n个物品从长到短进行排序,比较最短值和最长值的和与箱子长度进行比较即可

 #include<iostream>
#include<algorithm>
using namespace std;
int a[];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,l,i,j=;
int count=;
cin>>n>>l;
for( i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(i=n-;i>=;i--)
{
if(i==j) {count++;break;}
if(a[i]+a[j]<=l) j++;
count++;
if(i==j) break;
}
cout<<count<<endl;
if(t) cout<<endl;
}
return ;
}
 
 
 
 
 
 
 

Bin Packing的更多相关文章

  1. UVa 102 - Ecological Bin Packing(规律,统计)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. UVa - 102 - Ecological Bin Packing

    Background Bin packing, or the placement of objects of certain weights into different bins subject t ...

  3. Vector Bin Packing 华为讲座笔记

    Vector bin packing:first fit / best fit / grasp 成本:性价比 (先验) 设计评价函数: evaluation function:cosine simil ...

  4. UVA 1149 Bin Packing

    传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...

  5. 高效算法——Bin Packing F - 贪心

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Descripti ...

  6. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

  7. POJ2782:Bin Packing

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...

  8. UVA 1149 Bin Packing 二分+贪心

    A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...

  9. UVa 1149 (贪心) Bin Packing

    首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...

随机推荐

  1. 利用Roslyn构建一个简单的C#交互脚本引擎

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 微软的下一代编译器技术Roslyn是一个里程碑的技术,可以给.NET平台带来无限想象空间.比 ...

  2. CodeIgniter中驱动器的使用方法

    驱动器“Drivers”是CodeIgniter框架从2.0版本开始加入的新特性.正如中文版译者所言: 笔者看了这三篇英文参考,加上自己的一些理解,对官方文档关于驱动器的这一部分进行了一些补充. 1. ...

  3. PAT A 1014. Waiting in Line (30)【队列模拟】

    题目:https://www.patest.cn/contests/pat-a-practise/1014 思路: 直接模拟类的题. 线内的各个窗口各为一个队,线外的为一个,按时间模拟出队.入队. 注 ...

  4. NuGet学习笔记(1) 初识NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  5. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  6. for循环嵌套

    今天复习了分支语句以及for循环,新内容主要讲解了for循环的嵌套: 外循环控制行,内循环控制列 下面几个实例: 五行五列的矩阵 左下角是直角的三角形: 左上角是直角的三角形: 右上角为直角的三角形: ...

  7. hdu 5690 All x

    All X Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submiss ...

  8. CSS3详解:background

    CSS3对于background做了一些修改,最明显的一个就是采用设置多背景,不但添加了4个新属性,并且还对目前的属性进行了调整增强. 1.多个背景图片 在css3里面,你可以再一个标签元素里应用多个 ...

  9. AOP静态代理解析1-标签解析

    AOP静态代理使用示例见Spring的LoadTimeWeaver(代码织入) Instrumentation使用示例见java.lang.instrument使用 AOP的静态代理主要是在虚拟机启动 ...

  10. EBay .Net SDK Api 实践

        1.请求流程介绍   提供SOA地址:https://api.ebay.com/wsapi   WSDL生成的代码在WebService.cs文件当中.   ApiCall封装了所有的RPC, ...