Bin Packing
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>(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.
题意:
给定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的更多相关文章
- 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 ...
- Vector Bin Packing 华为讲座笔记
Vector bin packing:first fit / best fit / grasp 成本:性价比 (先验) 设计评价函数: evaluation function:cosine simil ...
- UVA 1149 Bin Packing
传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- POJ2782:Bin Packing
Description A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...
- 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
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
随机推荐
- W:Failed to fetch http://archive.ubuntukylin.com:10006/ubuntukylin/dists/pre
由于用ubuntu的时候装了几个ubuntukylin的软件(像搜狗拼音for linux),于是最近总是蹦出一个红色的三角提示,说无法更新,虽说不影响使用但是还是很不爽.解决方法记录如下: 进入系统 ...
- Vs2010工具栏显示“开始执行“按钮
转载来源:http://blog.csdn.net/fromhj/article/details/8795047 前言 在用visual studio 2010的时候,要运行程序,可以使用 1.菜单- ...
- Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)
题目链接: http://codeforces.com/problemset/problem/14/D 思路:直接枚举每一天路径的两端,然后求以每一端为树根的树上最长路径,然后相乘就可以了. # ...
- phpstorm的安装、破解、和汉化
1.去官网下载最新版安装. 2.完成选择购买软件,下面选择中间那个账户方法License server,把这条链接复制下去:http://idea.qinxi1992.cn 3.下载汉化包resour ...
- Eclipse配置Lifery SDK步骤与错误解决。
第一步.我们先去官网下载我们要用的东西 1.Eclipse 2.SDK 3.Tomcat 这是官网下载地址-->https://www.liferay.com/zh/downloads 下面是图 ...
- asp.net权限控制配置web.config
项目下 有三个文件夹 A,B,C 验正方式是 Forms 验正 我要设置他们的访问权限为, A,匿名可访问 B,普通用户授权后才能访问 C,只允许管理员访问 <configuration> ...
- PHP入门 - - 06-->HTML的表格标签
表格标签<table> <table>的属性: Align: left, center, right (表格的)位置 Border: ...
- SpringJDBC解析1-使用示例
JDBC(Java Data Base Connectivity,Java数据库连接)是一种用于执行SQL语句的JavaAPI,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组 ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
- [工作bug]一个weblogic跨应用导致session丢失的bug之旅
近来,发布一个应用,开发和本地测试一切都好,一旦部署到测试环境之后,坑爹的问题随之而来,应用程序不定时的超时,导致用户正在操作过程中被踢了出来,纠结了几天,终于在今天将此问题搞定: 1.系统架构 系统 ...