uva 1149:Bin Packing(贪心)
题意:给定N物品的重量,背包容量M,一个背包最多放两个东西。问至少多少个背包。
思路:贪心,最大的和最小的放。如果这样都不行,那最大的一定孤独终生。否则,相伴而行。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define N 100100 int a[N]; int main() {
int t;
scanf("%d", &t);
bool isfirst = true;
while (t--) {
if (isfirst) isfirst = false;
else puts("");
int n;
scanf("%d", &n);
int l;
scanf("%d", &l); for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a,a+n); int st = ;
int ed = n-;
int cnt = ;
while (st <= ed) {
if (st != ed){
if (a[st]+a[ed] <= l) {
st++;
ed--;
cnt++;
} else {
ed--;
cnt++;
}
} else {
cnt++;
ed--;
}
} printf("%d\n", cnt);
}
return ;
}
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 装箱(贪心)
每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...
- 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 ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- 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 ...
随机推荐
- 洛谷 P2279 [HNOI2003]消防局的设立
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- A * B Problem Plus HDU - 1402 (FFT)
A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ...
- CodeForces:#448 div2 B. XK Segments
传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...
- [BZOJ2947]促销(Splay)
Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...
- 4、python中的布尔值和None
一.布尔值 1.布尔值只有两个:True.Flase,表示python语句的真与假: 2.在python早期的版本,布尔值用1和0表示. 二.None 1.None表示虚无,什么也没有: 2.千万不要 ...
- Hyper-V 虚拟机快照:常见问题
发布时间: 2009年3月 更新时间: 2010年12月 应用到: Windows Server 2008 什么是虚拟机快照? 虚拟机快照可捕获正在运行的虚拟机的状态.数据和硬件配置. 快照有哪些用途 ...
- Python框架之Django学习笔记(四)
第一个基于Django的页面:Hello World 正如我们的第一个目标,创建一个网页,用来输出这个著名的示例信息:Hello world. 第一个视图 Hello world视图非常简单. 这些是 ...
- Windows网络编程笔记1
第一部分 传统网络API 传统的网络接口NetBIOS.重定向器.邮槽.命名管道等.第一,NetBIOS(Network Basic Input/Output System, NetBIOS)“网络基 ...
- error C2011: “Picture”:“struct”类型重定义
今天引用外来库时出现问题,也许是版本问题. 错误如下: .....\oursun\cincludes\quickdraw.h(309): error C2011: “Picture”:“struct” ...
- Pass Data Between ASP.NET Pages
There is the data to send in current page <asp:TextBox ID="DataToSendTextBox" runat=&qu ...