UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品
和之前做的独木舟上的旅行一样,注意一下格式就好了
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=;
int a[maxn]; int main() { int ncase,i,j,w,n,sum;
cin>>ncase;
int t=;
while(ncase--)
{
t++;
sum=;
memset(a,,sizeof(a));
cin>>n>>w;
for(i=;i<=n;i++) cin>>a[i]; sort(a+,a+n+);
i=;
j=n;
while(i<=j)
{
if(a[i]+a[j]<=w&&i!=j)
{
i++;
j--;
sum++;
}
else
{
j--;
sum++;
}
}
if(t!=) printf("\n");
printf("%d\n",sum);
}
return ;
}
UVa 1149 Bin Packing 【贪心】的更多相关文章
- 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 samele ...
- 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 ...
随机推荐
- Ubuntu下安装配置zsh和oh my zsh
zsh优势:自动补全功能强大和很高的可配置性 1.查看当前系统装了哪些shell cat /etc/shells 2.当前正在运行的是哪个版本的shell echo $SHELL 3.安装 ...
- FlashBuilder 4.7 破解方法
|举报|字号订阅 方法一: 方法问题: 能破解,但破解后启动FB需等待较长时间,等一两分钟吧. 操作步骤: 与其它Adobe CS6破解方法相同,覆盖安装目录下的amtlib.dll文件即可.(文 ...
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
引子: 本项目在老电脑上用的是oracle10g,换新电脑装的是oracle11g,但运行项目本没有什么关系,本来说创建个用户,用PLSQL手工导入数据,再改几下配置文件即可跑起来--但实际启动中遇到 ...
- 2014 Multi-University Training Contest 6
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uz2v.html Apple Tree http://acm.hdu.edu.cn/showpro ...
- .net framework 源码调试 与 问题解决
调试方式有二种, 看官方资料就OK. 官方地址: http://referencesource.microsoft.com/serversetup.aspx 1. 使用配置在线地址安装 2. 下载安装 ...
- soap 路由
下面主要通过项目实例来具体阐述如何实现wse路由和一些项目开发中的细节.本人水平有限,有不对的地方,请朋友们不吝赐教. 在开始项目之前,先了解一下路由的概念,所谓"路由",是指把数 ...
- JsRender系列demo(4)-if else
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- poj 3150 Cellular Automaton
首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...
- hdu 1713 相遇周期
求分数的最小公倍数.对于a/b c/d 先化简为最简分数,分数最小公倍数=分子的最小公倍数/分母的最大公约数. ;}
- centos下hadoop2.6.0集群搭建详细过程
一 .centos集群环境配置 1.创建一个namenode节点,5个datanode节点 主机名 IP namenodezsw 192.168.129.158 datanode1zsw 192.16 ...