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"
#include"algorithm"
using namespace std; const int maxn=100000+10; int n,l;
int a[maxn]; void Init()
{
cin>>n>>l;
for(int i=0;i<n;i++) cin>>a[i];
} void Work()
{
sort(a,a+n);
int j=0,ans=0;
for(int i=n-1;i>=0;i--)
{
if(i<j) break;
ans++;
while(a[i]+a[j]<=l&&i>j)
{
j++;
break;
}
}
cout<<ans<<endl;
} int main()
{
int T,t;
cin>>T;
t=T;
while(T--)
{
if(t!=T+1) cout<<endl;
Init();
Work();
}
return 0;
}

集训第四周(高效算法设计)F题 (二分+贪心)的更多相关文章

  1. 集训第四周(高效算法设计)I题 (贪心)

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  2. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  3. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

  4. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  5. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  6. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  7. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  8. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  9. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

随机推荐

  1. Springboot整合elasticsearch以及接口开发

    Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...

  2. POJ 2255 Tree Recoveryw(二叉树)

    题目原网址:http://poj.org/problem?id=2255 题目中文翻译: Description 小瓦伦丁非常喜欢玩二叉树. 她最喜欢的游戏是用大写字母构造的随机二叉树. 这是她的一个 ...

  3. ubuntu下进入xampp mysql命令行

    在命令行下进入到/opt/lampp/bin目录,使用命令:sudo ./mysql,回车即可. 注意:运行这个命令需要加上sudo,以root权限来运行,不然有些数据库没有权限查看.

  4. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  5. 题解报告:hdu 1062 Text Reverse

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 Problem Description Ignatius likes to write word ...

  6. 413 Arithmetic Slices 等差数列划分

    如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列.例如,以下数列为等差数列:1, 3, 5, 7, 97, 7, 7, 73, -1, -5, -9以下数列不是等差数列. ...

  7. maven 工程导入jar包

    Maven项目引入jar包的方法,希望能帮助有需要的朋友们 法一.手动导入:项目右键—>Build Path—>Configure Build Path—>选中Libraries—& ...

  8. 针对谷歌默认最小字体12px的正确解决方案

    利用css3的缩放,其最终大小就是:12px * 0.9(缩放比例) = 10.8px; 居然行得通.但回头一想,这么写的话,IE7 IE8会不会不兼容,还是12px呢?不出所料,果然不兼容.此时,又 ...

  9. 点击后打开QQ临时会话

    1.QQ官方提供的代码.如果没有加好友需要加好友才能聊天,也可以到这里http://shang.qq.com/v3/index.html 开通一个服务,同样可以实现临时会话. <a href=& ...

  10. Spring需要的几个关键配置文件(SSM框架整合)

    打包下载 springmvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...