zoukankan      html  css  js  c++  java
  • [PA 2014]Pakowanie

    Description

    你有n个物品和m个包。物品有重量,且不可被分割;包也有各自的容量。要把所有物品装入包中,至少需要几个包?

    Input

    第一行两个整数n,m(1<=n<=24,1<=m<=100),表示物品和包的数量。
    第二行有n个整数a[1],a[2],…,a[n](1<=a[i]<=10^8),分别表示物品的重量。
    第三行有m个整数c[1],c[2],…,c[m](1<=c[i]<=10^8),分别表示包的容量。

    Output

    如果能够装下,输出一个整数表示最少使用包的数目。若不能全部装下,则输出NIE。

    Sample Input

    4 3
    4 2 10 3
    11 18 9

    Sample Output

    2

    题解

    狗屎因为数组开小了,导致调了一个多小时...

    首先我们有贪心的思想就是背包肯定是从大往小选。

    再接着由于物品数$n<=24$,我们可以拿来状压。

    设$f[S]$表示当前物品集合为$S$,且当前背包所用容量为$f[S]$,$g[S]$为当前背包的编号,也就是背包数量。

    那么每次我们可以枚举一个物品加进来,如果没有炸掉当前背包,就可以直接填进去,否则就要开一个新背包,如果连新背包都开不下,那么这个转移就是不合法的。(参考wfj_2048

     1 //It is made by Awson on 2017.10.15
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <cmath>
     7 #include <stack>
     8 #include <queue>
     9 #include <vector>
    10 #include <string>
    11 #include <cstdio>
    12 #include <cstdlib>
    13 #include <cstring>
    14 #include <iostream>
    15 #include <algorithm>
    16 #define LL long long
    17 #define Min(a, b) ((a) < (b) ? (a) : (b))
    18 #define Max(a, b) ((a) > (b) ? (a) : (b))
    19 #define sqr(x) ((x)*(x))
    20 #define lowbit(x) ((-(x))&(x))
    21 using namespace std;
    22 const int N = 1<<24;
    23 const int INF = ~0u>>1;
    24 void read(int &x) {
    25     char ch; bool flag = 0;
    26     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    27     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    28     x *= 1-2*flag;
    29 }
    30 
    31 int n, m, st[N+5];
    32 int a[30], c[105];
    33 int f[N+5], g[N+5];
    34 
    35 bool comp(const int &q, const int &p) {
    36     return q > p;
    37 }
    38 
    39 void work() {
    40     read(n), read(m);
    41     memset(f, -1, sizeof(f));
    42     for (int i = 0; i < n; i++) read(a[i]), st[1<<i] = i;
    43     for (int i = 1; i <= m; i++) read(c[i]); sort(c+1, c+m+1, comp);
    44     for (int i = 0; i < n; i++) if (a[i] <= c[1]) f[1<<i] = c[1]-a[i], g[1<<i] = 1;
    45     for (int i = 1, lim = 1<<n; i < lim; i++) {
    46         if (f[i] == -1) f[i] = g[i] = INF;
    47         for (int t = i, j = i-lowbit(t); t && j; t -= lowbit(t), j = i-lowbit(t)) {
    48             if (f[j] == INF) continue;
    49             int bit = st[lowbit(t)];
    50             int ff, gg;
    51             if (f[j] >= a[bit]) ff = f[j]-a[bit], gg = g[j];
    52             else ff = c[g[j]+1]-a[bit], gg = g[j]+1;
    53             if (ff < 0) continue;
    54             if (gg < g[i]) f[i] = ff, g[i] = gg;
    55             else if (gg == g[i] && f[i] < ff) f[i] = ff;
    56         }
    57     }
    58     if (f[(1<<n)-1] == INF || g[(1<<n)-1] > m) printf("NIE
    ");
    59     else printf("%d
    ", g[(1<<n)-1]);
    60 }
    61 int main() {
    62     work();
    63     return 0;
    64 }
  • 相关阅读:
    阻止页面右键事件
    拖拽效果
    关于事件捕获
    放大镜效果
    如何不用border-radius 写圆角边框
    力扣—— Swap Nodes in Pairs(两两交换链表中的节点) python实现
    力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现
    力扣 ——Remove Duplicates from Sorted List II(删除排序链表中的重复元素 II)python实现
    力扣 — Rotate List()
    力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7674579.html
Copyright © 2011-2022 走看看