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;
}
}
 

poj 2782 Bin Packing (贪心+二分)的更多相关文章

  1. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

  2. uva 1149:Bin Packing(贪心)

    题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include < ...

  3. POJ 3122 Pie (贪心+二分)

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N ...

  4. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

  5. Bin Packing

    Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of  ...

  6. UVa 102 - Ecological Bin Packing(规律,统计)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  7. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  9. POJ 3190 Stall Reservations贪心

    POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...

随机推荐

  1. codevs 1222 信与信封问题(二分图的完美匹配)

    1222 信与信封问题   题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封. ...

  2. AOJ 0525 穷举

    题意:有一个烤饼器可以烤r行c列的煎饼,煎饼可以正面朝上(用1表示)也可以背面朝上(用0表示).一次可将同一行或同一列的煎饼全部翻转.现在需要把尽可能多的煎饼翻成正面朝上,问最多能使多少煎饼正面朝上? ...

  3. Parameterized tests

    Parameterized继承自Suite.Parameterized是在参数上实现了Suite,修饰一个测试类,然后提供多组构造函数的参数用于测试不同场景. import java.util.Arr ...

  4. 422. Valid Word Square

    似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...

  5. Log4j自定义Appender介绍

    转自:http://gemantic.iteye.com/blog/1234996 最初想要在执行一段业务逻辑的时候调用一个外部接口记录审计信息,一直找不到一个比较优雅的方式,经过讨论觉得log4j自 ...

  6. Html中src、href的相对路径与绝对路径

    What is a path? Why is this something developers should care about? A path is simply the location of ...

  7. AS3 读写 C++ 64位数字

    为框架添加了一套新的与C++通讯的数据协议,其中和C++的大爷们对于他们的64位数字(unsigned long long)读写的问题纠结了很久.真心觉得“学好C++走遍天下都不怕啊” AS里Numb ...

  8. [分享]WPF 虚拟键盘

    场景:用WPF做触屏的软件难免会需要用户输入的问题,至少是简单的数字,这个时候就免不了需要键盘输入. 思路:既然是虚拟键盘,那么我的目的就是模拟键盘输入即可. 1.模拟键盘输入 模拟键盘输入?那么肯定 ...

  9. linux两台server远程copy文件

    1.将远程机器的文件copy到本地 scp -r  root@10.10.15.25:/bea   /gg   将远程的/bea copy到/gg以下 -r  Recursively copy ent ...

  10. Qt深入:不能不知道的Type、Attribute和Flags

    Qter高手与新手的区别在于:知道还是不知道 Qt不是开发语言,所以无所谓谁厉害.但使用他的Qter却有着差异,也许是面向对象语言本身的.或者实际经验上的.而对于Qt本身来说,高手与新手最显著的差异在 ...