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(T--){
        scanf("%d%d",&n,&l);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        sort(a,a+n);
        int ans = 0;
        for(int i=0,j=n-1;i<=j;){
            if(a[i]+a[j]>l) {
                j--; ans++;
            }
            else {
                j--; i++; ans ++;
            }
        }
        printf("%d\n",ans);
        if(T) printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 05:05:07

1149 - Bin Packing(贪心)的相关文章

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

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

UVA 1149 Bin Packing 装箱(贪心,水)

每次选最大的物品和最小的物品放一起,如果放不下,大物体孤独终生,否则相伴而行... #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+42; int a[maxn]; template<class T> inline void scan_d(T *ret) { char c;*ret=0; while((c=getchar())<'0'||c>'9'); while(c>='0'&a

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

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)

UVa 1149 Bin Packing 题解

难度:α 用时:25 min 题目:?? 代码:?? 这是一道赤裸裸的贪心题.赤裸到和模版题差不多了. 所以下面长话短说. 朴素的算法是直接统计. 在这之上因为要贪心,所以做点小手段,记录一下可以被连带走的物体. 1 for (int i = 0; i < n; i++) { 2 if (taken[i]) continue; 3 ans++; 4 int iter = upper_bound(l, l+n, L-l[i]) - l; iter--; 5 while (iter > i &am

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 (贪心)

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