题意:给定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. english-phoneme

    1. 声音概述 2. 音素phoneme与音标 2.1 音素与音标 2.2 音素与字母 2.3 字母发音-字母自然发音对照表 2.4 音标表 2.5 元音字母-辅音字母表 2.6 单元音发音口形趋势表 ...

  2. [经验] Cocos Creator使用笔记 --- 俄罗斯方块 (1)

    一:  实现 物体匀速掉落 这是我在做俄罗斯方块的时候遇到的一个问题,  因为原来的方块的掉落是每秒掉落一个像素点, 但是这样看起来的话会是一卡一卡的, 为了让方块在掉落的过程中看起来更加的流畅, 于 ...

  3. 在linux环境中配置tomcat

    1.将tomcat文件上传到linux服务器上,并且解压文件. 2.创建文件夹 mkdir /usr/local/solr  (这个文件夹,根据自己的喜好来创建) 3.将解压好的tomcat文件复制到 ...

  4. List循环添加数据覆盖问题

    问题:java开发时,当我们使用List.add();循环添加数据,有时会出现前面添加的数据会被后面覆盖的现象.这是怎么回事尼? 会覆盖数据的代码 package com.boot.test; imp ...

  5. js取值问题----key为数字

    今天远程调用一个接口在处理返回的数据的时候突然发现数组的Key是一个数字 然后如果继续用“.”的话是不会的,会报错 要用中括号就可以解决

  6. C++ 11 :override 关键字的使用

    override 关键字 作用:在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数. 位置:函数调用运算符之后,函数体或纯虚函数标识 "= 0" ...

  7. SQLI_LAB------level 1

    SQLI_LAB 刷题刷题刷题!!! 知识扩展: SQL 1)SQL注入介绍 SQLI,sql injection,我们称之为 sql 注入.何为 sql,英文:Structured Query La ...

  8. Day5-T3

    原题目 要开运动会了,神犇学校的n个班级要选班服,班服共有100种样式,编号1~100.现在每个班都挑出了一些样式待选,每个班最多有100个待选的样式.要求每个班最终选定一种样式作为班服,且该班的样式 ...

  9. phpstrom+win10拼音输入法不跟随情况

    PHPSTORM拼中文时悬浮框一直在右下角,真是逼死强迫症的操作! 最好解决方案: 下载讯飞输入法,虽然有点不习惯,用着用着就行了 等待官方修复着bug吧; 网上说的直接下载jre64覆盖原来的版本也 ...

  10. 单选与多选与label

    单选radio和多选checkbox是用name属性关联的 相同的name就相当于同一道题 <input type="radio" name="radio" ...