POJ2782:Bin Packing
Description
A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l<tex2html_verbatim_mark> and each item i<tex2html_verbatim_mark> has length lil<tex2html_verbatim_mark> . We look for a minimal number of bins q<tex2html_verbatim_mark> such that
- each bin contains at most 2 items,
- each item is packed in one of the q<tex2html_verbatim_mark> bins,
- the sum of the lengths of the items packed in a bin does not exceed l<tex2html_verbatim_mark> .
You are requested, given the integer values n<tex2html_verbatim_mark> , l<tex2html_verbatim_mark> , l1<tex2html_verbatim_mark> , ..., ln<tex2html_verbatim_mark> , to compute the optimal number of bins q<tex2html_verbatim_mark> .
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The first line of the input file contains the number of items n<tex2html_verbatim_mark>(1n105)<tex2html_verbatim_mark> . The second line contains one integer that corresponds to the bin length l10000<tex2html_verbatim_mark> . We then have n<tex2html_verbatim_mark> lines containing one integer value that represents the length of the items.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For each input file, your program has to write the minimal number of bins required to pack all items.
Sample Input
1 10
80
70
15
30
35
10
80
20
35
10
30
Sample Output
6
Note: The sample instance and an optimal solution is shown in the figure below. Items are numbered from 1 to 10 according to the input order.
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int x[];
int main(){
int t;cin>>t;
int g=;
while(t--){
if(g++!=)puts("");
int n;cin>>n;int m;cin>>m;
int sum=;
for(int i=;i<n;i++){
scanf("%d",x+i);
}
sort(x,x+n);
int i=,j=n-;
while(i<j){
if(x[i]+x[j]<=m){
j--;i++;sum++;
}else{j--;sum++;}
}
if(i==j)sum++;
cout<<sum<<endl;
}
return ;
}
POJ2782: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 ...
- UVa - 102 - Ecological Bin Packing
Background Bin packing, or the placement of objects of certain weights into different bins subject t ...
- Vector Bin Packing 华为讲座笔记
Vector bin packing:first fit / best fit / grasp 成本:性价比 (先验) 设计评价函数: evaluation function:cosine simil ...
- UVA 1149 Bin Packing
传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the sa ...
- 高效算法——Bin Packing F - 贪心
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Descripti ...
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- UVa 1149 (贪心) Bin Packing
首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...
随机推荐
- 数据库数据用Excel导出的3种方法
将数据库数据用Excel导出主要有3种方法:用Excel.Application接口.用OleDB.用HTML的Tabel标签 方法1——Excel.Application接口: 首先,需要要Exce ...
- javascript 学习随笔1
html部分 <body onload="message()"><!--主题部分加载就调用-->document.getElementById(" ...
- USACO Palindromic Squares 【STL__string_的应用】
这里有个讲解 string 用法非常详细的博文:https://www.byvoid.com/zhs/blog/cpp-string 题目意思很简单啦,就是找回文 使用string可以高速A过 Sou ...
- 基于visual Studio2013解决算法导论之049活动选择问题
题目 活动选择问题 解决代码及点评 // 活动选择问题.cpp : 定义控制台应用程序的入口点. // #include<iostream> #define N 100 using ...
- 达内TTS6.0课件basic_day04
- PHP - 将HTML代码转义
代码: 页面显示: 页面源码:
- 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)
再说字符串 一.字符串的编码 字符串的编码是个很令人头疼的问题,由于计算机是美国人发明的,他们很理所当然的认为计算机只要能处理127个字母和一些符号就够用了,所以规定了一个字符占用8个比特(bit)也 ...
- 06-OC分类、协议、ARC
目录: 一.分类 二.扩展 三.协议 四.内存管理ARC 回到顶部 一.分类 1 分类就是类的补充和扩展,本质上是类的一部分,把一个类分成若干部分,每个部分就是分类. 2 语法 * 文件中的语法@in ...
- python binary lib on win/各种python库的二进制包
虽然很不愿意在windows上折腾python,但有时linux不在身边的时候还得在windows上写代码.很久之前找到这个地址,后来忘了,今天搜了半天才把它找到,所以记录一下. http://www ...
- cocos2d-x游戏开发系列教程-中国象棋00-前言
象棋描述 在说代码之前,我们先让象棋效果登场,以方便大家对代码的理解 欢迎界面 中国象棋程序,运行起来的第一个界面是一个欢迎界面,该欢迎界面在停留一秒后进入游戏界面 游戏主界面 新局:所有棋子归位,状 ...