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 ...
随机推荐
- [resource]Github上维护的一个机器学习相关的框架,库和工具列表
https://github.com/josephmisiti/awesome-machine-learning A curated list of awesome Machine Learning ...
- 2437: [Noi2011]兔兔与蛋蛋 - BZOJ
Description Input 输入的第一行包含两个正整数 n.m.接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母" ...
- 1068: [SCOI2007]压缩 - BZOJ
Description 给一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复信息.压缩后的字符串除了小写字母外还可以(但不必)包含大写字母R与M,其中M标记重复串的开始,R重复从上一 ...
- c++ 时间格式化
struct tm tm_time; strptime(time.c_str(), "%Y%m%d %H:%M:%S", &tm_time); time_t tt = mk ...
- [转载]点评阿里云、盛大云等国内IaaS产业
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:刘黎明 原文地址:http://www.chinacloud.org ...
- 怎么让CentOS集群自动同步时间
怎么让CentOS集群自动同步时间?首先机器要连外网,这样才能从互联网上同步时间,这是首先要了解的.好了,主要的方法如下: 在除了运行ntpd之外的机器上,执行: [html] # chkconfig ...
- uva 10859
刘书例题 树形dp #include <cstdio> #include <cstdlib> #include <cmath> #include <map& ...
- linux源代码阅读笔记 fork和execve的区别
1. man exec就可以知到: The exec() family of functions replaces the current process image with a new proce ...
- IOS委托设计模式(摘自IOS开发指南)
- Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...