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", &t);
    bool isfirst = true;
    while (t--) {
        if (isfirst) isfirst = false;
        else puts("");
        int n;
        scanf("%d", &n);
        int l;
        scanf("%d", &l);

        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
        }
        sort(a,a+n);

        int st = 0;
        int ed = n-1;
        int cnt = 0;
        while (st <= ed) {
            if (st != ed){
                if (a[st]+a[ed] <= l) {
                    st++;
                    ed--;
                    cnt++;
                } else {
                    ed--;
                    cnt++;
                }
            } else {
                cnt++;
                ed--;
            }
        }

        printf("%d\n", cnt);
    }
    return 0;
}
时间: 2024-12-21 20:39:50

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

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

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 题解

难度:α 用时: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

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)

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 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0