[PA2014]Pakowanie

题目大意:

\(n(n\le24)\)个物品和\(m(m\le100)\)个背包,每个物体有一个体积\(a_i\),每个背包有一个容量\(c_i\)。问装完所有物品至少需要几个包?

思路:

一个贪心的策略是优先装大的包,显然这样可以最少化所用背包的数量。

将所有的背包按照容量从大到小排序,\(f[s]\)表示装了物品的状态为\(s\),用了前\(f[s]\)大的背包。\(g[s]\)表示装了物品的状态为\(s\),最后那个背包用了\(g[s]\)的容量。

随便转移就好了。

时间复杂度\(\mathcal O(2^nn)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
#include<functional>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=24,M=101;
int a[N],c[M],f[1<<N],g[1<<N];
int main() {
const int n=getint(),m=getint();
for(register int i=0;i<n;i++) a[i]=getint();
for(register int i=1;i<=m;i++) c[i]=getint();
std::sort(&c[1],&c[m]+1,std::greater<int>());
std::fill(&f[1],&f[1<<n],INT_MAX);
for(register int i=1;i<1<<n;i++) {
for(register int j=0;j<n;j++) {
if(!((i>>j)&1)) continue;
const int k=i^(1<<j);
const int tmp1=g[k]+a[j]<=c[f[k]]?f[k]:f[k]+1;
const int tmp2=tmp1==f[k]?g[k]+a[j]:a[j];
if(tmp2>c[tmp1]) continue;
if(tmp1<f[i]) {
f[i]=tmp1;
g[i]=INT_MAX;
}
if(tmp1==f[i]) g[i]=std::min(g[i],tmp2);
}
}
if(f[(1<<n)-1]==INT_MAX) {
puts("NIE");
return 0;
}
printf("%d\n",f[(1<<n)-1]);
return 0;
}

[PA2014]Pakowanie的更多相关文章

  1. bzoj3717: [PA2014]Pakowanie

    Time Limit: 90 Sec Memory Limit: 256 MBSubmit: 128 Solved: 43[Submit][Status][Discuss]Description 你有 ...

  2. bzoj 3717: [PA2014]Pakowanie

    Description 你有n个物品和m个包.物品有重量,且不可被分割:包也有各自的容量.要把所有物品装入包中,至少需要几个包? Input 第一行两个整数n,m(1<=n<=24,1&l ...

  3. 【bzoj3717】[PA2014]Pakowanie 状压dp

    题解: 自己在这一类问题上想到的总是3^n的枚举法 首先背包从大到小排序 f[i]表示搞出为i的状态至少要用几个背包,g[i]表示最大剩余容量 这样就可以2^n*n 因为这么做利用了状态之间的先后顺序 ...

  4. bzoj3717 [PA2014]Pakowanie 贪心+状压DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3717 题解 这道题大概也就只能算常规的状压 DP 吧,但是这个状态和转移的设计还是不是很好想. ...

  5. Work at DP

    转载请注明出处:http://www.cnblogs.com/TSHugh/p/8858805.html Prepared: (无notes的波兰题目的notes见我的波兰题目补全计划)BZOJ #3 ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

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

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

  8. BZOJ 3721: PA2014 Final Bazarek

    3721: PA2014 Final Bazarek Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 645  Solved: 261[Submit][ ...

  9. BZOJ 3709: [PA2014]Bohater

    3709: [PA2014]Bohater Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1050  Solved: ...

随机推荐

  1. 20165227 《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    20165227 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:朱越 学号:20165227 指导教师:娄 ...

  2. 64_t7

    texlive-ulqda-bin-svn13663.0-33.20160520.fc26.2..> 24-May-2017 15:57 33102 texlive-ulqda-doc-svn2 ...

  3. docker 部署 portainer(http)

    =============================================== 2019/4/30_第6次修改                       ccb_warlock 更新 ...

  4. Python基础(1):dir(),help()

    Python:3.6.4 开始编写Python程序了...可是,某个模块怎么用呢?模块里的函数怎么用呢?...使用本文介绍的dir().help()两个帮助函数可以 获得绝大部分开发所需要的信息! d ...

  5. IntelliJ IDEA 去除IDE自动的参数名 提示功能

  6. HDU 3068 最长回文(manacher模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...

  7. Es官方文档整理-3.Doc Values和FieldData

    Es官方文档整理-3.Doc Values和FieldData 1.Doc Values 聚合使用一个叫Doc Values的数据结构.Doc Values使聚合更快.更高效且内存友好. Doc Va ...

  8. python类型学习

    python类型学习 标准类型 数字 Integer 整型 Boolean 布尔型 Long integer 长整型 Floating point real numer  浮点型 Complex nu ...

  9. 如何在k8s集群里快速运行一个镜像?

    在docker里,快速run一个镜像,很简单的. k8s的世界,与之类似. 但要注意一下,如果镜像本身没有提供command命令,这个容器由于前台输出完成,很快就退出了. 所以,遇到这种镜像,就最好自 ...

  10. Metronic 5.0.5 bootstrap后台管理模板

    演示地址:http://keenthemes.com/preview/metronic/    下载 Dashboard Table