这个题是在二分的题单上的,可是依据二分法写出来的会在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. sublime配置python

    Sublime Text 2作为一款轻量级的编辑器,特点鲜明.方便使用,愈发受到普罗大众的喜爱.我个人近期也開始用了起来.同一时候,我近段时间还在学习Python的相关东西.所以開始用ST2来写Pyt ...

  2. rgba

    正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  3. url与图片

    http://restapi.amap.com/v3/staticmap?location=116.481485,39.990464&zoom=10&size=750*300& ...

  4. 字典(dictionary)与映射(map)

    1. 字典:key-value 键值对 反转字典:reverse_dict = dict(zip(D.values(), D.keys())) 前提要保证 D 的 value 不会出现重复,因为字典反 ...

  5. jFinal 关联数据库操作

    1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table create table CLASSES ( classesid NUMBER not null, cl ...

  6. 洛谷P3355 骑士共存问题

    题目描述 在一个 n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置 ...

  7. Vue或React多页应用脚手架

    https://github.com/zhujiasheng/vue-multipage https://github.com/MeCKodo/vue-multipage

  8. vi命令常用操作

    一.vi的操作模式  vi提供两种操作模式:输入模式(insert mode)和指令模式(command mode).在输入模式下,用户可输入文本资料.在指令模式下,可进行删除.修改等各种编辑动作.  ...

  9. 重装python 和 yum

    https://blog.csdn.net/ghostyusheng/article/details/https://segmentfault.com/q/1010000009194060/a-102 ...

  10. 03014_properties配置文件

    1.使用properties配置文件 (1)开发中获得连接的4个参数(驱动.URL.用户名.密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可: (2)通常情况下 ...