UVA - 1149 Bin Packing(装箱)(贪心)
题意:给定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(装箱)(贪心)的更多相关文章
- 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
传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- UVA-1149 Bin Packing (贪心)
题目大意:给定n个物品的重量,无限个容量为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 ...
随机推荐
- 使用input选择本地图片,并且实现预览功能
1.使用input标签选择本地图片文件 用一个盒子来存放预览的图片 2.JS实现预览 首先添加一个input change事件,再用到 URL.createObjectURL() 方法 用来创建 UR ...
- GoJS简单示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- js左右选项移动
<!--网页代码--><div class="modal" id="modal-primary7"> <div class=&qu ...
- volume 方式使用 Secret【转】
Pod 可以通过 Volume 或者环境变量的方式使用 Secret,今天先学习 Volume 方式. Pod 的配置文件如下所示: ① 定义 volume foo,来源为 secret mysecr ...
- 侯捷C++学习(一)
//c++学习//标准库非常重要//要规范自己的代码complex c1(2,1);complex c2;complex* pc = new complex(0,1);string s1(" ...
- Docker 学习之镜像导入导出及推送阿里云服务器(三)
在前面两节里主要就是记录一些docker的基本的操作,包括搜索镜像,拉取镜像,根据镜像创建容器等等,在这一节主要就是记录Docker对于镜像文件的导入导出,及推送到阿里云再从阿里云获取镜像. 一.镜像 ...
- docker学习笔记-02:docker常用命令
一.帮助命令: 1.查看版本:docker version 2.查看信息:docker info 3.查看帮助信息:docker --help 二.镜像命令: (一).查看已有镜像: 1.命令:doc ...
- Day7 - K - Biorhythms POJ - 1006
Some people believe that there are three cycles in a person's life that start the day he or she is b ...
- 文献阅读报告 - Social BiGAT + Cycle GAN
原文文献 Social BiGAT : Kosaraju V, Sadeghian A, Martín-Martín R, et al. Social-BiGAT: Multimodal Trajec ...
- ①spring简介以及环境搭建(一)
注*(IOC:控制反转.AOP:面向切面编程) spring官网:http://spring.io/ spring简介: spring是一个开源框架 spring为简化企业级应用开发而生,使用Spri ...