【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 ...
随机推荐
- php计算两个时间相差的天数、小时数、分钟数、秒数
$startdate="2011-3-15 11:50:00";//开始时间 $enddate="2012-12-12 12:12:12";//结束时间 $da ...
- 紫书 习题 10-13 UVa 11526(打表找规律+分步枚举)
首先看这道题目,我预感商数肯定是有规律的排列的,于是我打表找一下规律 100 / 1 = 100 100 / 2 = 50 100 / 3 = 33 100 / 4 = 25 100 / 5 = ...
- iOS 全局修改UINavigation 后退按钮
将导航栏的后退按钮中的文字去掉一直是老大难问题,现在可以使用运行时机制,将后退按钮文字清空 创建UINavigationItem的category,直接上代码 .h文件 #import <UIK ...
- Myeclipse学习总结(4)——Eclipse常用开发插件
(1) AmaterasUML 介绍:Eclipse的UML插件,支持UML活动图,class图,sequence图,usecase图等:支持与Java class/interf ...
- 洛谷 P2005 A/B Problem II
P2005 A/B Problem II 题目背景 为了让大家紧张的心情放松一下,这一题题是一道非常简单的题目. 题目描述 给出正整数N和M,请你计算N div M(N/M的下取整). 输入输出格式 ...
- [B cannot be cast to java.lang.String
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.inv ...
- QThread 爬坑之旅(三种办法解决QObject: Cannot create children for a parent that is in a different thread)
Cannot create children for a parent that is in a different thread. 在Qt的官方文档,大家知道有两种方式使用QThread. You ...
- setTimeOut函数传参数
这样使用,后面的4000无效 setTimeout(removeGift(customer_id,gift_id),4000); function removeGift(customer_id,gif ...
- 配置CiscoWorks 2000 ANI同步
配置CiscoWorks 2000 ANI同步 在CiscoWorks 2000的LAN ManagementSolution(LMS)中,Cisco包含了一种ANI的自动发现过程和Res ...
- css3 文字溢出 换行实现方案
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...