ACM Boxing problem

Source: Internet
Author: User

Description

A set of n<tex2html_verbatim_mark> 1-dimensional items has A to is packed in identical bins. All bins has exactly the same length l<tex2html_verbatim_mark> and each item i<tex2ht Ml_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 the most 2 items,
    • Each item was 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 was followed by a blank line, and there was also a blank line between the consecutive inputs.


The first line of the input file contains the number of items n<tex2html_verbatim_mark>(1n ) <tex2html_verbatim_mark>. The second line contains one integer so corresponds to the bin length l10000<tex2html_verbatim_mark> . We then had 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 the consecutive cases would be separated to a blank line.


For each input file, your program has a to write the minimal number of bins required to pack all items.

Sample Input

1108070153035108020351030

Sample Output

6

Problem Solving Ideas:
The main idea of this topic is that given the length of the block and the length of the box that can be used for the block, it is required that a box can fit up to two blocks, asking us to find out how many boxes we need to use. We first put the boxes in order from large to small, and then let a current largest box and a minimum box combination to see if it can be loaded into the box, if not, then we only loaded into the big box, otherwise the two boxes are loaded. (We can use X, y to separate control from left to right and right to left subscript) notice the blank line of the title, I just keep hanging on this empty line.
Program code:
#include <iostream>#include<algorithm>using namespacestd;inta[100005];intFunintAintBB) {    returnA>b;}intMain () {intC; CIN>>C; intg=1;  while(c--)    {        if(g++!=1) cout<<Endl; intn,l; CIN>>N; CIN>>l;  for(intI=0; i<n;i++) {cin>>A[i]; } sort (A,a+n,fun); intx=0; inty=n-1; ints=0;  while(x<y) {if(a[x]+a[y]<=l) {x++; Y--; S++; }            Else{x++; S++; }        }         if(x==y) s++; cout<<s<<Endl; }    return 0;}

ACM Boxing problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.