首页 > 其他 > 详细

Bin Packing 贪心

时间:2015-08-05 22:02:25      阅读:197      评论:0      收藏:0      [点我收藏+]
F - 贪心+ 二分
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

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 li技术分享l<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>(1技术分享n技术分享105)<tex2html_verbatim_mark> . The second line contains one integer that corresponds to the bin length l技术分享10000<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.

 

技术分享
解题思路:这个题的题意给一个l,再输入n个数,判断在输入的n数中,最多两数和小于等于l,即这个题目,首先排序,然后从最大一边开始判断,最大和最小的相加是否小于等于l,若成立,则缩小范围,完成一对匹配,若不成立,则将些数单独作为一个,往下继续判断。
技术分享
#include <cstdio>
#include <algorithm>
using namespace std;
int a[100010];
int n,l;
int init()
{
    sort(a,a+n);
    int i,c=0, j=0;
    for(i=n-1;i>=0;i--)
    {
        if(i<j) break;
        c++;
        while(a[i]+a[j]<=l&&i>j)
        {
            j++;
            break;
        }
    }
    return c;
}
int main()
{
    int t,f=0;
    scanf("%d",&t);
    while(t--)
    {
        if(f>0) printf("%d\n");
        f++;
                scanf("%d%d",&n,&l);
              for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
              printf("%d\n",init());
    }

    return 0;

}
View Code

 

Bin Packing 贪心

原文:http://www.cnblogs.com/www-cnxcy-com/p/4705850.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!