UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same
length l and each item i has length li ≤ l. We look for a minimal number of bins q such that
• each bin contains at most 2 items,
• each item is packed in one of the q bins,
• the sum of the lengths of the items packed in a bin does not exceed l.
You are requested, given the integer values n, l, l1, . . . , ln, to compute the optimal number of bins
q.
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 (1 ≤ n ≤ 105
). The second line
contains one integer that corresponds to the bin length l ≤ 10000. We then have n lines containing one
integer value that represents the length of the items.
Output
For each test case, your program has to write the minimal number of bins required to pack all items.
The outputs of two consecutive cases will be separated by a blank line.
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.
Sample Input
1
10
80
70
15
30
35
10
80
20
35
10
30
Sample Output
6
题意:
给定N(N<=100000) 个物品的重量,背包的容量M,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品
题解:
我们二分背包个数
check的时候先自大到小填一个在每一个背包
再从大到小填满,检查是否放完就好了
还有就是注意输出格式
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll; int n,m,a[N],b[N];
bool check(int x) {
int cnt = ;
if(x*<n) return ;
for(int i=n;i>=n-x+;i--) {
b[++cnt] = a[i];
}
b[++cnt] = m;
int sum=cnt;
for(int i=n-x;i>=;i--) {
if(b[--sum]+a[i]>m) {
// cout<<b[sum]<<" "<<a[i]<<endl;
return ;
}
}
return ;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
if(n==) {
puts("");
continue;
}
sort(a+,a+n+);
//cout<<check(6)<<endl;return 0;
int l=, r=n,ans=n;
while(l<=r) {
int mid=(l+r)/;
if(check(mid)) r=mid-,ans=mid;
else l=mid+;
}
// cout<<1<<endl;
printf("%d\n",ans);
if(T)cout<<endl;
}
}
UVA 1149 Bin Packing 二分+贪心的更多相关文章
- UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- UVA 1149 Bin Packing 装箱(贪心)
每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...
- uva 1149:Bin Packing(贪心)
题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...
- 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 ...
- 【hoj】2160 bin packing 二分、贪心
这个题是在二分的题单上的,可是依据二分法写出来的会在oj上超时.依据题目以下给出的提示能够发现能通过贪心法每次都找最能满足的情况去填充每个包,这样就能保证使用的包的数量是最少的 二分法解法: #inc ...
- UVa 1335 Beijing Guards (二分+贪心)
题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物. 析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么 ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- UVA-1149 Bin Packing (贪心)
题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最 ...
随机推荐
- 大数据攻城狮之Linux基础------rpm软件管理
rpm的英文名称为: Redhat package manager 常用的命令加组合: i 安装 rpm -ivh 软件包名 当然我们的rpm也可以支持多包同时操作 rpm -ivh 软件包1 软件包 ...
- firewalld使用
1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...
- Lucene中Analyzer语句分析
Lucene中Analyzer语句分析,利用lucene中自带的词法分析工具Analyzer,进行对句子的分析. 源代码如下: package com.test; import java.io.IOE ...
- ubuntu-设置分辨率
xrandr -s 1440x900 -r 60 前提是,分辨率选项中有对应的设置选项.
- 函数的arguments
1.arguments a.只能在函数内部被访问. b.arguments是一个伪数组(有下标索引,可以存放多个值,但是他里面没有数组的方法.) c.arguments里面存的是什么?函数的实际参数传 ...
- sql变量需要加小括号
declare @num int select top (@num) * from A --注意,使用变量来查询的时候,单个变量需要使用()
- matlab中的@函数
原文链接:http://blog.sina.com.cn/s/blog_5e73a8fc0100t9yg.html 这是个函数句柄 @(x,y) 表示未知数是x和y punct - Funct ...
- ML二:NNSearch数据结构--二叉树
wiki百科:http://zh.wikipedia.org/wiki/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AD%A6%E4%B9%A0 opencv学习笔记--二杈决策树: ...
- sql 查询多列 小于某值
select COUNT(*) from ( select ID,H1 AS Value from Table_1 union all select ID,H2 from Table_1 union ...
- 如何在vue里面正确的引用 jquery 和 第三方插件
vue-cli webpack全局引入jquery(jq通过npm安装的并非本地文件) 1.首先在package.json里加入, dependencies:{ "jquery" ...