这个题是在二分的题单上的,可是依据二分法写出来的会在oj上超时。依据题目以下给出的提示能够发现能通过贪心法每次都找最能满足的情况去填充每个包,这样就能保证使用的包的数量是最少的

二分法解法:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#define MAX 100000 using namespace std;
int n,length;
int l[MAX];
bool cmp(int a,int b)
{
return a<b;
}
bool judge(int mid)
{
int r = 0,q = 0;
int max = 0;
int tag[n];//标记该物是否之前就打包过了
memset(tag,0,sizeof(tag));
for(int i = 0;i < n;i++){
r = length - l[i];
max = i;
for(int j = n-1;j > i;j--){
if((l[j] < r) &&!tag[j]){//尽量找到最能填满剩余空间的物
max = j;
break;
}
}
if(!tag[max]){
q++;
tag[max] = 1;
}
}
if(q < mid)
return false;
else
return true;
}
int main()
{
int high,low,mid,res;
while(cin>>n){
cin>>length;
for(int i = 0;i < n;i++){
cin>>l[i];
}
sort(l,l+n,cmp);
high = n;
low = 0;
res = 0;
while(low <= high){
mid = (low+high)/2;
if(judge(mid)){
res = mid;
low = mid+1;
}
else
high = mid-1;
}
cout<<res<<endl;
}
return 0;
}

贪心法解法:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std; int li[100003];
int ca[100003]; bool cmp(int a,int b)
{
return a>b;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
int l; while(scanf("%d",&n)!=EOF)
{
scanf("%d",&l);
for(int i=0;i<n;i++)
{
scanf("%d",&li[i]);
ca[i] = l;
} sort(li,li+n,cmp); int ans = 0;
for(int i=0,j=n-1;i<=j;i++)
{
if(li[i] + li[j] <= l)
{
j--;
}
ans++;
}
printf("%d\n",ans);
}
return 0;
}

【hoj】2160 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. 高效算法——Bin Packing F - 贪心

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Descripti ...

  3. UVA-1149 Bin Packing (贪心)

    题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最 ...

  4. UVa 1149 Bin Packing 【贪心】

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

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

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

  6. Bin Packing

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

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

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

  8. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  9. UVa - 102 - Ecological Bin Packing

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

随机推荐

  1. caioj 1067动态规划入门(一维一边推5: 乘积最大(高精度版))

    因为这里涉及到乘号的个数,那么我们可以用f[i][j]表示前i个位乘号为j个时的最大乘积 那么相比上一题就是多了一层枚举多少个乘号的循环,可以得出 f[i][r] = max(f[j - 1][r - ...

  2. P1872 回文串计数(回文树)

    题目描述 小a虽然是一名理科生,但他常常称自己是一名真正的文科生.不知为何,他对于背诵总有一种莫名其妙的热爱,这也促使他走向了以记忆量大而闻名的生物竞赛.然而,他很快发现这并不能满足他热爱背诵的心,但 ...

  3. Apache CXF实战之二 集成Sping与Web容器

    本文链接:http://blog.csdn.net/kongxx/article/details/7525481 Apache CXF实战之一 Hello World Web Service 书接上文 ...

  4. ArcGIS api for javascript——加载图标

    描述 这个示例展示了如何能用一个动画图片显示地图正在加载.在这个示例中,图片是一个小的动画GIF.当地图第一次加载或用户缩放和平移地图时显示图片.当所有图层加载完成图片消失. 这个示例是通过event ...

  5. 利用NSProxy解决NSTimer内存泄漏问题

    之前写过一篇利用RunTime解决由NSTimer导致的内存泄漏的文章,最近和同事讨论觉得这样写有点复杂,然后发现有NSProxy这么好用的根类,根类,根类,没错NSProxy与NSObject一样是 ...

  6. File的getPath()和getAbsolutePath()和getCanonicalPath()的差别

    这几个方法是有一次无意的发现,我当时也不知道什么意思,就百度了,查到了一些列子: 原文地址http://www.blogjava.net/dreamstone/archive/2007/08/08/1 ...

  7. CSS3 实现RSS图标

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CSS3 实现RSS图标&l ...

  8. POJ 3193 字符串排序+比较

    思路: 1. 先把那m个排个序 此后每回二分+strncmp一下就好了 strncmp是个好东西啊-- 2. hash判判 (注意 hash会有冲突--------.) //By SiriusRen ...

  9. js实现日期转换方法

    //方法1function timeStamp1String(time) { var datetime = new Date(); datetime.setTime(time); var year = ...

  10. Codefroces 852 G. Bathroom terminal

    G. Bathroom terminal Smith wakes up at the side of a dirty, disused bathroom, his ankle chained to p ...