poj 2782 Bin Packing (贪心+二分)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
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<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
int a[];
int cmp(int x, int y)
{
return x>y;
}
int main()
{
int n,i,l,t;
cin>>t;
while(t--)
{
cin>>n>>l;
for(i = ; i < n; i++)
cin>>a[i];
sort(a,a+n,cmp);
int k=,j=n-;
for(i=; i<n; i++)
{
if(i>j)break;
k++;
if(i<j&&a[i]+a[j]<=l)
{
j--;
}
}
printf("%d\n",k);
if(t)cout<<endl;
}
}
最新文章
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
- CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx 1.6.2)
- 20150706 test2
- SVN操作手册
- bitset常用函数用法记录 (转载)
- python函数参数前面单星号(*)和双星号(**)的区别
- 以Lockbits的方式访问bitmap
- 【转】科普Spark,Spark是什么,如何使用Spark
- git入门超详细(转载)
- HTML5和CSS3实例教程[总结二]
- winzip15.0注冊码
- 【Owin 学习系列】2. Owin Startup 类解析
- Ubuntu上安装VMware tools
- python sys.argv[]简单解释
- mysql查询某一个字段是否包含中文字符
- [No0000C3]StarUML2 全平台破解方法
- QT编写的网页浏览器网页乱码解决方法
- supervisord 进程管家
- linux下&;、nohup与screen的比较
- 解决 Microsoft Excel has stopped working
热门文章
- DFS 10.1.5.253 1501
- Android应用开发学习之画廊视图
- JAX-WS + Spring 开发webservice
- android 时间对话框 TimePickerDialog简介
- 在Eclipse中使用JUnit4进行单元測试(0基础篇)
- oracle中imp命令具体解释
- Struts2 Spring hibernate 整合示例 .
- iOS 如何优雅的处理“回调地狱Callback hell”(一) (下)
- CALayer的基本操作
- lvs+heartbeat搭建负载均衡高可用集群