题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品

和之前做的独木舟上的旅行一样,注意一下格式就好了

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include <cmath>
  5. #include<stack>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<algorithm>
  11. #define mod=1e9+7;
  12. using namespace std;
  13.  
  14. typedef long long LL;
  15. const int maxn=;
  16. int a[maxn];
  17.  
  18. int main()
  19.  
  20. {
  21.  
  22. int ncase,i,j,w,n,sum;
  23. cin>>ncase;
  24. int t=;
  25. while(ncase--)
  26. {
  27. t++;
  28. sum=;
  29. memset(a,,sizeof(a));
  30. cin>>n>>w;
  31. for(i=;i<=n;i++) cin>>a[i];
  32.  
  33. sort(a+,a+n+);
  34. i=;
  35. j=n;
  36. while(i<=j)
  37. {
  38. if(a[i]+a[j]<=w&&i!=j)
  39. {
  40. i++;
  41. j--;
  42. sum++;
  43. }
  44. else
  45. {
  46. j--;
  47. sum++;
  48. }
  49. }
  50. if(t!=) printf("\n");
  51. printf("%d\n",sum);
  52. }
  53. return ;
  54. }

UVa 1149 Bin Packing 【贪心】的更多相关文章

  1. uva 1149:Bin Packing(贪心)

    题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...

  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 装箱(贪心)

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

  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. [resource]Github上维护的一个机器学习相关的框架,库和工具列表

    https://github.com/josephmisiti/awesome-machine-learning  A curated list of awesome Machine Learning ...

  2. 2437: [Noi2011]兔兔与蛋蛋 - BZOJ

    Description Input 输入的第一行包含两个正整数 n.m.接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母" ...

  3. 1068: [SCOI2007]压缩 - BZOJ

    Description 给一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复信息.压缩后的字符串除了小写字母外还可以(但不必)包含大写字母R与M,其中M标记重复串的开始,R重复从上一 ...

  4. c++ 时间格式化

    struct tm tm_time; strptime(time.c_str(), "%Y%m%d %H:%M:%S", &tm_time); time_t tt = mk ...

  5. [转载]点评阿里云、盛大云等国内IaaS产业

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:刘黎明      原文地址:http://www.chinacloud.org ...

  6. 怎么让CentOS集群自动同步时间

    怎么让CentOS集群自动同步时间?首先机器要连外网,这样才能从互联网上同步时间,这是首先要了解的.好了,主要的方法如下: 在除了运行ntpd之外的机器上,执行: [html] # chkconfig ...

  7. uva 10859

    刘书例题  树形dp #include <cstdio> #include <cstdlib> #include <cmath> #include <map& ...

  8. linux源代码阅读笔记 fork和execve的区别

    1. man exec就可以知到: The exec() family of functions replaces the current process image with a new proce ...

  9. IOS委托设计模式(摘自IOS开发指南)

  10. Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)

    本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...