POJ2782:Bin Packing

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.

将所有物品由大到小排序,从大到小装入物品,如果某个装了一个物品的BIN的余量可以再装入一个物品,就把物品转入,否则就申请一个新的BIN.

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int x[100002];
int main(){
    int t;cin>>t;
    int g=1;
    while(t--){
        if(g++!=1)puts("");
        int n;cin>>n;int m;cin>>m;
        int sum=0;
        for(int i=0;i<n;i++){
            scanf("%d",x+i);
        }
        sort(x,x+n);
        int i=0,j=n-1;
        while(i<j){
            if(x[i]+x[j]<=m){
                j--;i++;sum++;
            }else{j--;sum++;}
        }
        if(i==j)sum++;
        cout<<sum<<endl;
    }
return 0;
}

时间: 2024-10-29 10:47:48

POJ2782:Bin Packing的相关文章

POJ 2782 Bin Packing

 Bin Packing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5416   Accepted: 2452 Description A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l and each item i has length li<=l

UVA 1149 Bin Packing

传送门 A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l and each item i has length li ≤ l. We look for a minimal number of bins q such that • each bin contains at most 2 items, • each item is pa

Bin Packing 贪心

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<te

UVA 1149 Bin Packing 二分+贪心

A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samelength l and each item i has length li ≤ l. We look for a minimal number of bins q such that• each bin contains at most 2 items,• each item is packed in

1149 - Bin Packing(贪心)

这道题可以用贪心算法来实现,对所有物品的长度进行排序,然后先看当前最短的和最长的能否装进一个袋子里,如果不能,这个当前最大的物品要单独放一个袋子,如果可以,将这两个物品放一个袋子,然后i++:j--:重复上述操作. #include<bits/stdc++.h> using namespace std; const int maxn = 100000 + 5; int T,n,l,a[maxn]; int main(){ scanf("%d",&T); while(

UVA-1149 Bin Packing (贪心)

题目大意:给定n个物品的重量,无限个容量为m的箱子,每个箱子最多装两个物品,要把所有的物品都装下,最少需要多少个箱子. 题目分析:贪心策略:每次将最重和最轻的两个物品放到一个箱子里,如果装不下,则将最重的单独装到一个箱子里. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; int a[100005]

uva 1149:Bin Packing(贪心)

题意:给定N物品的重量,背包容量M,一个背包最多放两个东西.问至少多少个背包. 思路:贪心,最大的和最小的放.如果这样都不行,那最大的一定孤独终生.否则,相伴而行. 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 100100 int a[N]; int main() { int t; scanf("%d", &a

【hoj】2160 bin packing 二分、贪心

这个题是在二分的题单上的,可是依据二分法写出来的会在oj上超时.依据题目以下给出的提示能够发现能通过贪心法每次都找最能满足的情况去填充每个包,这样就能保证使用的包的数量是最少的 二分法解法: #include <iostream> #include <stdio.h> #include <cstring> #include <algorithm> #define MAX 100000 using namespace std; int n,length; in

UVa1149 Bin Packing (贪心)

链接:http://vjudge.net/problem/UVA-1149 分析:贪心的放,先放重的,剩下的容量看能不能放进一个轻的. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 100000 + 5; 6 7 int a[maxn]; 8 9 int main() { 10 int T; 11 scanf("%d", &T)