F - 贪心+ 二分

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.

 
使用贪心,先模拟贪心过程:给出一个桶,那么这个桶能装两个就两个
具体实现:把数组从大到小排序,然后使用两个变,一个在左,一个在右,首先以左边为据,在左边尽量找一个和它放在一个桶里,如果成功就有i++和j--,直到i和j相遇,跳出循环,统计这时已拿出的桶的数量,即为装进去成功的桶的数量
 
 
 
#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;
}
}
 

最新文章

  1. java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
  2. CentOS7 编译安装 Nginx (实测 笔记 Centos 7.0 + nginx 1.6.2)
  3. 20150706 test2
  4. SVN操作手册
  5. bitset常用函数用法记录 (转载)
  6. python函数参数前面单星号(*)和双星号(**)的区别
  7. 以Lockbits的方式访问bitmap
  8. 【转】科普Spark,Spark是什么,如何使用Spark
  9. git入门超详细(转载)
  10. HTML5和CSS3实例教程[总结二]
  11. winzip15.0注冊码
  12. 【Owin 学习系列】2. Owin Startup 类解析
  13. Ubuntu上安装VMware tools
  14. python sys.argv[]简单解释
  15. mysql查询某一个字段是否包含中文字符
  16. [No0000C3]StarUML2 全平台破解方法
  17. QT编写的网页浏览器网页乱码解决方法
  18. supervisord 进程管家
  19. linux下&amp;、nohup与screen的比较
  20. 解决 Microsoft Excel has stopped working

热门文章

  1. DFS 10.1.5.253 1501
  2. Android应用开发学习之画廊视图
  3. JAX-WS + Spring 开发webservice
  4. android 时间对话框 TimePickerDialog简介
  5. 在Eclipse中使用JUnit4进行单元測试(0基础篇)
  6. oracle中imp命令具体解释
  7. Struts2 Spring hibernate 整合示例 .
  8. iOS 如何优雅的处理“回调地狱Callback hell”(一) (下)
  9. CALayer的基本操作
  10. lvs+heartbeat搭建负载均衡高可用集群