【Luogu】P3052摩天大楼里的奶牛(状压DP)
2024-09-01 04:27:36
参见ZHT467的题解。
f[i]表示在i这个集合下的最少分组数和当前组最少的容量。
从1到(1<<n)-1枚举i,对于每个i枚举它的子奶牛,然后重载运算符计算。
代码如下
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<algorithm>
using namespace std; inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} long long w[];
int Max;
struct Node{
long long cnt,v;
}f[];
int n,W;
inline Node operator +(const Node a,const int b){
return a.v+b<=W?(Node){a.cnt,a.v+b}:(Node){a.cnt+,b};
} inline bool operator <(const Node a,const Node b){
if(a.cnt!=b.cnt) return a.cnt<b.cnt;
return a.v<b.v;
} inline Node min(Node a,Node b){ return a<b?a:b; } int main(){
n=read(),W=read();
Max=(<<n)-;
for(int i=;i<=n;++i) w[i]=read();
for(int i=;i<=Max;++i){
f[i]=(Node){,W};
for(int j=;j<=n;++j){
if(((<<(j-))&i)==) continue;
f[i] = min(f[i],f[(<<(j-))^i]+w[j]);
}
}
printf("%lld",f[Max].cnt+);
return ;
}
最新文章
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
- Android编码规范04
- ASIHttpRequest 使用理解
- Microsoft Help Viewer
- App.config的学习笔记
- hdu-5680 zxa and set(水题)
- WindowsMediaPlayer控件批量添加文件至播放列表
- [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
- Oracle性能分析11:系统统计信息
- 转 delphi SelText,GetText,SetText用法
- LIS算法
- POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)
- compress.go
- 五、JAVA反射、线程
- Python项目部署-使用Nginx部署Django项目
- js中的所有鼠标事件 键盘事件
- AWS EC2 使用root账户密码登陆
- MFC 控件使用教程
- Cacti Install Error
- S导入部门数据 更新父部门、责任人