题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品。求至少要多少个背包才能装下所有的物品。

分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个重量最轻的,就再装一个重量最轻的,依此类推。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int main(){
int T;
scanf("%d", &T);
while(T--){
int n, l;
scanf("%d%d", &n, &l);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
sort(a, a + n);
int cnt = 0;
int st = 0, et = n - 1;
while(st <= et){
if(a[et] <= l){
if(st < et && a[et] + a[st] <= l){
++st;
}
--et;
++cnt;
}
}
printf("%d\n", cnt);
if(T) printf("\n");
}
return 0;
}

  

UVA - 1149 Bin Packing(装箱)(贪心)的更多相关文章

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

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

  2. UVA 1149 Bin Packing 二分+贪心

    A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...

  3. UVa 1149 Bin Packing 【贪心】

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

  4. uva 1149:Bin Packing(贪心)

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

  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. 高效算法——Bin Packing F - 贪心

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

  7. UVA-1149 Bin Packing (贪心)

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

  8. UVa 102 - Ecological Bin Packing(规律,统计)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  9. UVa - 102 - Ecological Bin Packing

    Background Bin packing, or the placement of objects of certain weights into different bins subject t ...

随机推荐

  1. java#keytool#生成私钥证书库、公钥证书库

    原文,向作者致敬

  2. 修剪草坪 HYSBZ - 2442

    在一年前赢得了小镇的最佳草坪比赛后,FJ变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,FJ希望能够再次夺冠. 然而,FJ的草坪非常脏乱,因此,FJ只能够让他的奶牛来完成这项工作.F ...

  3. pyhon 内置函数

    chr()   asci码 dir()  目录,显示目录. divmod(10,3)   返回商和余数   例如  (3, 1)   返回的为一个元组    可以用于分页 enumerate()    ...

  4. springboot中文官方文档

    springboot中文官方文档 https://www.breakyizhan.com/springboot/3028.html spring框架 https://www.breakyizhan.c ...

  5. Html5 -- 语义标签兼容性处理

    方法一:通过js处理 方法二:完美的处理方式 no.1 !--[if lte IE 8]> <script type="text/javascript" src=&qu ...

  6. Linux下四种安装软件方式

    1.yum源安装 可以解决依赖关系,但不确定安装的位置 2.rpm 基础安装 要自己解决依赖问题 rpm -ivh 安装 rpm -uvh 更新 rpm -e --nodeps 卸载    取消依赖 ...

  7. arm linux 移植 udhcp 与 使用

    背景 在一些网络环境下,需要静态IP不够现实,需要使用DHCP进行自动获取IP地址. udhcpc是一个面向嵌入式系统的非常小的DHCP客户端,字母的缩写微μ- DHCP -客户端client(μDH ...

  8. 序列化sys随笔补充

    sys是在和python解释器做交互 sys.path----模块查找的顺序 sys.path.append() sys.path.insert()sys.argv---只能在终端执行 sys.arg ...

  9. shiro缓存配置

    realm的缓存 方法一: 在securityManager配置中添加cacheManager配置项,会注入到realm中. 方法二:在realm中配置. realm本身实现了CacheManager ...

  10. mysql导入导出无权限

    error:The MySQL server is running with the --secure-file-priv option so it cannot execute this state ...