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 二分+贪心的更多相关文章

  1. UVa 1149 Bin Packing 【贪心】

    题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...

  2. UVA 1149 Bin Packing 装箱(贪心)

    每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...

  3. uva 1149:Bin Packing(贪心)

    题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...

  4. UVA - 1149 Bin Packing(装箱)(贪心)

    题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品.求至少要多少个背包才能装下所有的物品. 分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个 ...

  5. UVA 1149 Bin Packing

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

  6. 【hoj】2160 bin packing 二分、贪心

    这个题是在二分的题单上的,可是依据二分法写出来的会在oj上超时.依据题目以下给出的提示能够发现能通过贪心法每次都找最能满足的情况去填充每个包,这样就能保证使用的包的数量是最少的 二分法解法: #inc ...

  7. UVa 1335 Beijing Guards (二分+贪心)

    题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物. 析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么 ...

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

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

  9. UVA-1149 Bin Packing (贪心)

    题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最 ...

随机推荐

  1. hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图

    题目分析: 一个人要不是爱狗讨厌猫的人,要不就是爱猫讨厌狗的人.一个人喜欢的动物如果离开,那么他也将离开.问最多留下多少人. 思路: 爱猫和爱狗的人是两个独立的集合.若两个人喜欢和讨厌的动物是一样的, ...

  2. hdu3572Task Schedule 最大流,判断满流 优化的SAP算法

    PS:多校联赛的题目质量还是挺高的.建图不会啊,看了题解才会的. 参考博客:http://blog.csdn.net/luyuncheng/article/details/7944417 看了上面博客 ...

  3. zmodem使用方法

    无论有xshell还是secureCRT连接linux的时. 默认都用一个zmodem可以帮助window和linux之间传输文件 很方便和实用的工具. 不过默认是无法使用的 需要安装lrzsz软件 ...

  4. associatedtype关联类型

    associatedtype关联类型   定义一个协议时,有的时候声明一个或多个关联类型作为协议定义的一部分将会非常有用.关联类型为协议中的某个类型提供了一个占位名(或者说别名),其代表的实际类型在协 ...

  5. mvc关于pots请求 哪个函数 出现bug研究

    这样能请求home下的updateA函数 这样能请求home下的update函数,不能请求updateA函数

  6. ZBrush关于遮罩的一些操作

    本文讨论使用ZBrush®软件如何在屏幕上创建遮罩和操纵遮罩. 1. 绘制遮罩 按下Ctrl键你就能够在你的模型上绘制遮罩(笔刷的笔划的开始和结束都必须在模型上),默认情况下,遮罩区域在模型上显示为一 ...

  7. java equals的用法

    equals方法,用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较.Object类中的equals方法内部使用的就是==比较运算符. package Xuexi; public cla ...

  8. Day 13迭代器生成器

    迭代器 1.迭代器就是迭代的工具,迭代也可以说成是重复,并且每一次重复都是基于上一次的结果而来的,在python中一切皆对象. 2.可迭代对象:只要拥有__iter__方法的对象都是可迭代对象. 3. ...

  9. django rest-farme-work 的使用(1)

    Django REST框架是一个用于构建Web API的强大且灵活的工具包 您可能想要使用REST框架的一些原因: 可浏览性 身份认证 支持ORM和非ORM的序列化 良好的文档支持 安装初步 pip ...

  10. Swoole 源码分析——基础模块之 Pipe 管道

    前言 管道是进程间通信 IPC 的最基础的方式,管道有两种类型:命名管道和匿名管道,匿名管道专门用于具有血缘关系的进程之间,完成数据传递,命名管道可以用于任何两个进程之间.swoole 中的管道都是匿 ...