【hoj】2160 bin packing 二分、贪心
这个题是在二分的题单上的,可是依据二分法写出来的会在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 二分、贪心的更多相关文章
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- UVA-1149 Bin Packing (贪心)
题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最 ...
- UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- UVA 1149 Bin Packing 装箱(贪心)
每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... 答案变得更优是因为两个物品一起放了,最大的物品是最难匹配的,如果和最小的都放不下的话,和其它匹配也一定放不下了. # ...
- Bin Packing
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...
- UVa 102 - Ecological Bin Packing(规律,统计)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- UVa - 102 - Ecological Bin Packing
Background Bin packing, or the placement of objects of certain weights into different bins subject t ...
随机推荐
- uname---用于打印当前系统相关信息
uname命令用于打印当前系统相关信息(内核版本号.硬件架构.主机名称和操作系统类型等). 语法 uname(选项) 选项 -a或--all:显示全部的信息: -m或--machine:显示电脑类型: ...
- python在leecode刷题-第一题和第七题
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] num ...
- 熟悉Android开发不得不知道的技巧
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 1.用Eclipse插件将文件批量编码如GBK-UTF-8 用 ...
- Launcher3实现壁纸居中
Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,仅仅能确保第一次开机时壁纸居中,后面用户改动桌面数目后,就无法达到了.怎样要在默认桌面数目配置时居中 ...
- OpenStack云桌面系列【1】—開始
关于"云桌面"和"桌面云" 首先,wiki里面是没有关于"桌面云"和"云桌面"的定义和其他信息. 百度百科 ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- 用Hexo搭建个人博客
博客地址: http://astraylinux.com/ 文章地址: http://astraylinux.com/2015/06/02/linux-Init-Hexo/ Step 1 Instal ...
- C++ 鼠标模拟程序
关于鼠标模拟程序应用不算少见.在游戏外挂或者一些操作频繁位置确定的程序上应用往往有奇效. 比較旧的API是mouse_event,本人一開始也用这个在搞,只是后来才看到新的API在操作上更加统一.稍作 ...
- C语言keywordstatic的绝妙用途
为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量.在变量类型的前面加上就自己主动清0了.还有就是加上statickeyword的,无论是变量 ...
- input file HTML控件控制
网页上添加一个input file HTML控件: 1 <input id="File1" type="file" /> 默认是这样的,所有文件类型 ...