zoukankan      html  css  js  c++  java
  • UVA 1149 Bin Packing(装箱)(贪心)

    题意:给定N(N<=10^5)个物品的重量Li,背包的容量M,同时要求每个背包最多装两个物品。求至少要多少个背包才能装下所有的物品。

    分析:先排序,从最重的开始装,如果重量小于M,则如果能装一个重量最轻的,就再装一个重量最轻的,依此类推。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    const int MAXN = 1e5 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN];
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            int n, l;
            scanf("%d%d", &n, &l);
            for(int i = 0; i < n; ++i){
                scanf("%d", &a[i]);
            }
            sort(a, a + n);
            int cnt = 0;
            int st = 0, et = n - 1;
            while(st <= et){
                if(a[et] <= l){
                    if(st < et && a[et] + a[st] <= l){
                        ++st;
                    }
                    --et;
                    ++cnt;
                }
            }
            printf("%d\n", cnt);
            if(T) printf("\n");
        }
        return 0;
    }
    

      

  • 相关阅读:
    浅析一类要求相邻不同的环上染色问题
    中国剩余定理(CRT)及其扩展(ExCRT)
    bsoj5988 [Achen模拟赛]期望 题解
    涂色游戏 题解
    [JZOJ A组]球 题解
    由 [SDOI2012]Longge的问题 探讨欧拉函数和莫比乌斯函数的一些性质和关联
    [NOIP模拟]文本编辑器 题解
    Nilearn 小记
    django 开发笔记1
    浅谈无需工作量证明的加密货币
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6373981.html
Copyright © 2011-2022 走看看