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. jQuery不熟点总结

     jQuery 事件 1 .trigger() 方法触发被选元素的指定事件类型. 2 .delegate() 事件委派  1.不占内存2.可以给未来元素(后期动态添加的元素)添加事件. 2.  添加元 ...

  2. 第7章 性能和可靠性模式 Failover Cluster(故障转移群集)

    上下文 您已经决定在设计或修改基础结构层时使用群集以提供高度可用的服务. 问题 您应该如何设计一个高度可用的基础结构层,来防止因单台服务器或它所运行的软件出现故障而导致的服务丢失? 影响因素 在设计高 ...

  3. showdialog

    在C#中窗口的显示有两种方式:模态显示(showdialog)和非模态显示(show). 区别: 模态与非模态窗体的主要区别是窗体显示的时候是否可以操作其他窗体.模态窗体不允许操作其他窗体,非模态窗体 ...

  4. vue2.0.js

     数据的渲染.数据同步  组件化.模块化 路由    ajax  数据流 Vue.js学习资源        中文官网:http://cn.vuejs.org/             源码:http ...

  5. Android Fragment 初步解析

    Fragment经常在我们的开发中见到,但是自我感觉对Fragment的理解还是处于初级的阶段,接下来我将用几篇文章尽量深的解析Fragment 让我们开始吧!!! Fragment的生命周期 Fra ...

  6. 图的连通性问题的小结 (双连通、2-SAT)

    图的连通性问题包括: 1.强连通分量. 2.最小点基和最小权点基. 3.双连通. 4.全局最小割. 5.2-SAT 一.强连通分量 强连通分量很少单独出题,一般都是把求强连通分量作为缩点工具. 有三种 ...

  7. 2017/01/07 学习笔记 jar包,maven

    ① 关于tar,jar,war文件 tar是通用的另一种打包格式,为了部署到服务器时方便. jar是java app server识别的java部署格式,其实是Zip文件,只是内部的文件有规范. wa ...

  8. linux中errno使用(转)

    当linux中的C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因,在实际编程中用这一招解决了不少 ...

  9. python编写简单的html登陆页面(2)

    1  在python编写简单的html登陆页面(1)的基础上在延伸一下: 可以将动态分配数据,实现页面跳转功能: 2  跳转到新的页面:return render_template('home1.ht ...

  10. 了解http协议

    http:超文本传输协议 https:安全超文本传输协议 FTP:文件传输协议 TCP:网络控制协议 IP:互联网协议 UDP:用户数据协议 https协议特点-------------------- ...