题意:给定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(贪心)的更多相关文章

  1. UVA 1149 Bin Packing 二分+贪心

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

  2. UVa 1149 Bin Packing 【贪心】

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

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

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

  4. UVA - 1149 Bin Packing(装箱)(贪心)

    题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品.求至少要多少个背包才能装下所有的物品. 分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个 ...

  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. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

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

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

  8. UVa - 102 - Ecological Bin Packing

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

  9. Bin Packing

    Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of  ...

随机推荐

  1. 精通SpringBoot--整合druid监控SQL执行

    咳咳,今天我们做些简单而实用的东西,使用springboot 整合alibaba的driud数据库监控工具.alibaba已经提供了spring-boot-starter的jar包了.先看pom.xm ...

  2. python中函数返回多个值

    用ipython运行情况如下: # 新建一个函数 In [1]: def nums(): ...: a = 11 ...: b = 22 ...: c = 33 ...: return [a,b,c] ...

  3. Python之路--Python初识

    Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程 ...

  4. B1020 月饼(25 分)

    B1020 月饼(25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意 ...

  5. [Uva623]500!(高精)

    Description 求N! \(N \leq 1000\) Sample Input 10 30 50 100 Sample Output 10! 3628800 30! 265252859812 ...

  6. C# Redis存Session Hash存对象

    1.新建一个控制台程序,并新建一个类“UserInfo” 2.从github下载redis的windows服务 https://github.com/ServiceStack/redis-window ...

  7. “帮你APP”团队冲刺1

    1.整个项目预期的任务量 (任务量 = 所有工作的预期时间)和 目前已经花的时间 (所有记录的 ‘已经花费的时间’),还剩余的时间(所有工作的 ‘剩余时间’) : 所有工作的预期时间:88h 目前已经 ...

  8. Helloworld 在jvm 内存图

    HelloWorld.java源码如下:   public class HelloWorld { public static void main(String[] args) { String s ; ...

  9. Creating Hyperv Agent Installer

    Creating Hyperv Agent Installer   Skip to end of metadata   Created by Anshul Gangwar, last modified ...

  10. 59、佳博wifi打印机怎么配置

    1.去这里下载配置软件(注意,需要再windows下进行)http://pan.baidu.com/s/1bn1y4FX,并解压安装程序 2.连上wifi打印机的热点,比如说佳博打印机的默认为Gpri ...