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 ≤ 10^5 ). 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
------------------------------------------------
首先注意输出格式,UVA的题常常要求The outputs of two consecutive cases will be separated by a blank line.巨坑。
Solution:
Greedy,我的解法是从小到大枚举长度,将每个长度与另一个尽可能大的长度装在一起,若找不到就将这个长度单独装。
Proof:
反证法。假设存在一种更优的方案,其中某个装有两个物品i, j (设length(i)<=length(j))的容器b中的较短的那个物品i不是与能和它装在一起的最长物品k装在一起的,调换k和j的位置,得到的是同样优的方案。所以上述贪心策略导致最优方案。
Implementation:
实现是用map模拟,太low了。
#include <bits/stdc++.h>
using namespace std;
map<int,int> cnt;
int main(){
int T; scanf("%d", &T);
for(int n, l, cs=; T--;){
if(cs++) puts("");
scanf("%d%d", &n, &l);
for(int len; n--;) scanf("%d", &len), cnt[len]++;
int ans=;
for(int now; !cnt.empty();){
now=cnt.begin()->first;
ans++;
cnt.begin()->second--;
if(!cnt.begin()->second){
cnt.erase(now);
}
if(cnt.empty()) break;
auto it=cnt.lower_bound(l-now);
if(it==cnt.end()) it--;
for(;;it--){
if(it->first<=l-now){
it->second--;
if(!it->second){
cnt.erase(it->first);
}
break;
}
if(it==cnt.begin()) break;
}
}
printf("%d\n", ans);
}
}
P.S. 我本想用multiset模拟的,但后来发现multiset不支持单个删除相同元素。
multiset<int> s;
int main(){
s.insert();
s.insert();
printf("%d\n", s.size()); //
s.erase();
printf("%d\n", s.size()); //
}
UVA 1149 Bin Packing的更多相关文章
- 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 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- uva 1149:Bin Packing(贪心)
题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...
- UVA 1149 Bin Packing 装箱(贪心)
每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...
- UVA - 1149 Bin Packing(装箱)(贪心)
题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品.求至少要多少个背包才能装下所有的物品. 分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个 ...
- 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 ...
- Bin Packing
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...
- Vector Bin Packing 华为讲座笔记
Vector bin packing:first fit / best fit / grasp 成本:性价比 (先验) 设计评价函数: evaluation function:cosine simil ...
随机推荐
- 17SpringMvc_在业务控制方法中写入包装User的模型来收集参数——解决问题
在解决问题之前,我要说明一下jsp页面上填入信息,一个用户的信息比如用户的名字,用户的电话,用户的手机等等,在这个jsp页面上填好信息后,转到有个action处理这个信息.原理是什么? 在jsp页面上 ...
- Windows客户端C/C++编程规范“建议”——风格
本文来自:http://blog.csdn.net/breaksoftware/article/details/37935459 命名风格也非常适用于C# 9 风格 9.1 优先使用匈牙利命名法 等级 ...
- 微服务架构:Eureka集群搭建
版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 服务注册.发现是微服务架构的关键原理之一,由于微服务架构是由一系列职责单一的细粒度服务构成的网状结构,服务之间通过轻量机制进行通信,这就必 ...
- ES5基础01:正则表达式
1.功能 匹配特定模式:比如匹配手机号码,匹配身份证号码等 替换文本:比如将input中的空格全部去掉 提取字符串:将特定的字符串提取出来 2.语法
- Python快速教程 尾声(转)
原文地址: http://www.cnblogs.com/vamei/p/3603046.html 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留 ...
- [CareerCup] 1.8 String Rotation 字符串的旋转
1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...
- 如何下载Hibernate
官网: http://hibernate.org/ 打开hibernate官网,选择Hibernate ORM,点击左侧的Downloads 点击Downloads后,可以看到如下页面,右侧是各个版本 ...
- 学习笔记:Twitter核心数据类库团队的Hadoop优化经验
一.来源 Streaming Hadoop Performance Optimization at Scale, Lessons Learned at Twitter (Data platform @ ...
- android加固签名工具(源码下载)
背景 每次android加固了都要命令行签名好麻烦,正好之前做了个图标生成工具. 所以改了改,比写批处理还要省事. 原理 其实就是用winform程序调用控制台执行命令,android签名的命令如下 ...
- 服务发现:Zookeeper vs etcd vs Consul
[编者的话]本文对比了Zookeeper.etcd和Consul三种服务发现工具,探讨了最佳的服务发现解决方案,仅供参考. 如果使用预定义的端口,服务越多,发生冲突的可能性越大,毕竟,不可能有两个服务 ...