zoukankan      html  css  js  c++  java
  • 高效算法——Bin Packing F

     
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    Download as PDF
     

    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$ le$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$ le$n$ le$105)<tex2html_verbatim_mark> . The second line contains one integer that corresponds to the bin length l$ le$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.

    epsfbox{p3503.eps}
    解题思路:这个题的题意给一个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
    ");
            f++;
                    scanf("%d%d",&n,&l);
                  for(int i=0;i<n;i++)
                    scanf("%d",&a[i]);
                  printf("%d
    ",init());
        }
    
        return 0;
    
    }
    View Code
    版权声明:此代码归属博主, 请务侵权!
  • 相关阅读:
    9.经典进程同步问题
    8.信号量
    7.进程同步之临界区问题
    6.CPU调度
    AIX 10G HA RAC卸载
    HP11.31安装11.2.0.3实施手册
    AIX上安装oracle10g
    习水医院12C RAC 数据库安装文档
    linux 11201(11203) ASM RAC 安装
    如果一个节点重新安装了,处理办法
  • 原文地址:https://www.cnblogs.com/www-cnxcy-com/p/4705850.html
Copyright © 2011-2022 走看看