题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=3717

题解

这道题大概也就只能算常规的状压 DP 吧,但是这个状态和转移的设计还是不是很好想。

首先很显然,要优先把物品往大的包里面装,直到装不了别人再去装下一个。

可以考虑贪心的策略,如果这个背包还能塞下的话,那么一定要去塞,这样至少是不会差的。

所以令 \(dp[S]\) 表示要装下 \(S\) 中的东西需要多少背包,\(f[S]\) 表示如果要达到 \(dp[S]\) 的结果,那么最后一个背包最多还有多少剩余的空间。

最后 \(dp[Fullset]\) 就是答案。


时间复杂度 \(O(n2^n)\),但是时间限制很充足。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 24 + 7;
const int M = 100 + 7;
const int NP = (1 << 24) + 7;
const int INF = 0x3f3f3f3f; int n, m, S;
int a[N], c[M];
int dp[NP], f[NP]; inline void work() {
std::sort(c + 1, c + m + 1, std::greater<int>());
S = (1 << n) - 1;
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int s = 0; s <= S; ++s) {
if (dp[s] == INF) continue;
for (int i = 1; i <= n; ++i) if (!((s >> (i - 1)) & 1)) {
int ss = s | (1 << (i - 1));
if (f[s] < a[i]) {
if (c[dp[s] + 1] >= a[i])
if (smin(dp[ss], dp[s] + 1)) f[ss] = c[dp[ss]] - a[i];
else if (dp[ss] == dp[s] + 1) smax(f[ss], c[dp[ss]] - a[i]);
}
else {
if (smin(dp[ss], dp[s])) f[ss] = f[s] - a[i];
else if (dp[ss] == dp[s]) smax(f[ss], f[s] - a[i]);
}
}
}
if (dp[S] != INF) printf("%d\n", dp[S]);
else puts("NIE");
} inline void init() {
read(n), read(m);
for (int i = 1; i <= n; ++i) read(a[i]);
for (int i = 1; i <= m; ++i) read(c[i]);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj3717 [PA2014]Pakowanie 贪心+状压DP的更多相关文章

  1. POJ 1795 DNA Laboratory (贪心+状压DP)

    题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...

  2. BZOJ4560 JLOI2016字符串覆盖(kmp+贪心+状压dp+单调队列)

    首先kmp求出每个子串能放在哪些位置.接下来的两部分贪心和状压都可以,各取比较方便的. 最大值考虑贪心.考虑枚举子串的左端点出现顺序,在此基础上每个子串的位置肯定都应该尽量靠前,有是否与上个子串有交两 ...

  3. CodeForces165E 位运算 贪心 + 状压dp

    http://codeforces.com/problemset/problem/165/E 题意 两个整数 x 和 y 是 兼容的,如果它们的位运算 "AND" 结果等于 0,亦 ...

  4. [bzoj3717][PA2014]Pakowanie_动态规划_状压dp

    Pakowanie bzoj-3717 PA-2014 题目大意:给你n个物品m个包,物品有体积包有容量,问装下这些物品最少用几个包. 注释:$1\le n\le 24$,$1\le m\le 100 ...

  5. 2018.08.29 NOIP模拟 movie(状压dp/随机化贪心)

    [描述] 小石头喜欢看电影,选择有 N 部电影可供选择,每一部电影会在一天的不同时段播 放.他希望连续看 L 分钟的电影.因为电影院是他家开的,所以他可以在一部电影播放过程中任何时间进入或退出,当然他 ...

  6. Codeforces 429C Guess the Tree(状压DP+贪心)

    吐槽:这道题真心坑...做了一整天,我太蒻了... 题意 构造一棵 $ n $ 个节点的树,要求满足以下条件: 每个非叶子节点至少包含2个儿子: 以节点 $ i $ 为根的子树中必须包含 $ c_i ...

  7. 刷题总结——树有几多愁(51nod1673 虚树+状压dp+贪心)

    题目: lyk有一棵树,它想给这棵树重标号. 重标号后,这棵树的所有叶子节点的值为它到根的路径上的编号最小的点的编号. 这棵树的烦恼值为所有叶子节点的值的乘积. lyk想让这棵树的烦恼值最大,你只需输 ...

  8. 洛谷P2831 愤怒的小鸟——贪心?状压DP

    题目:https://www.luogu.org/problemnew/show/P2831 一开始想 n^3 贪心来着: 先按 x 排个序,那么第一个不就一定要打了么? 在枚举后面某一个,和它形成一 ...

  9. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

随机推荐

  1. linux centos添加yum镜像

    下载配置文件,加入/etc/yum.repos.d/阿里镜像:http://mirrors.aliyun.com/repo/Centos-7.repo网易镜像:http://mirrors.163.c ...

  2. 在成为测试大牛的路上,我推荐BestTest

    BestTest-Python自动化测试9月份班开始招生啦! 网络+现场同步进行,课程新升级,web自动化+接口自动化双管齐下,一线互联网测试开发工程师带你在自动化的世界里自由翱翔! 推荐优惠多多,欢 ...

  3. 20180715-Java日期时间

    import java.util.Date;public class DateDemo{ public static void main(String[] args){ //初始化Date对象 Dat ...

  4. C++ Standard Template Library (STL) 高级容器

    更多 STL 数据结构请阅读 NOIp 数据结构专题总结(STL structure 章节) std::map Definition: template < class Key, // map: ...

  5. drawChild中画阴影,裁剪出圆角

    我们在EasyConstraintLayout中初始化paint,并且关闭硬件加速,然后在drawChild中实现阴影逻辑,最终代码如下. public class EasyConstraintLay ...

  6. 术语-PM:PM/项目管理 百科

    ylbtech-术语-PM:PM/项目管理 百科 PM项目管理(Project Management),是以项目为对象的系统管理方法,通过一个临时性的.专门的柔性组织,对项目进行高效率的计划.组织.指 ...

  7. leetcode 171. Excel表列序号(python)

    给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...

  8. poj2010 Moo University - Financial Aid 优先队列

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

  9. 2019/10/13 TZOJ

    水题虽不好,但是很爽 渴望未来某天能把剩下的题补了,先做个记录. Hard Disk Drive http://acm.hdu.edu.cn/showproblem.php?pid=4788 单位转化 ...

  10. 继承Process类,另一种方法计算累加和以及阶乘

    #定义一个类 继承Process类 from multiprocessing import Process import os import time class jiecheng(Process): ...