当前位置:网站首页>简单易懂的子集dp
简单易懂的子集dp
2022-04-23 06:21:00 【Zimba_】
前置知识:
状压:
用二进制(或n进制)代表的十进制数来表示状态,通过位操作进行状态调整。
举个例子,房间内4盏灯的亮灭情况为0101,该状态存储下的十进制数为5。
从右往左编号为0,1,2,3。打开第3盏灯即将原状态5变为5|(1<<3)。
关于状压只做简单介绍。
枚举二进制子集:
二进制数为s,枚举其子集的含义是指,枚举所有x∈{x| x|s=s}。
具体操作如下:
for (int i = s; i; i = (i - 1) &s)
这样就可以精确地枚举s的二进制子集,注意此时循环体中子集不包含0(空集),如要添加,需手动设置循环条件。
正文:
概述:
顾名思义,子集dp的状态转移,通常是由子集转移至当前集合,而有了枚举二进制子集的枚举方法之后,代码写起来会非常舒服,可以拥有更优秀的复杂度。
框架:
用转移方程dp[S]=dp[S⊕T]+max(dp[T]),T⊆S且T≠Ø为例,其中S⊕T含义是T关于S的补集,大体框架如下。
for(int i=0;i<1<<n;i++)
{
for(int j=S&(S-1);j;S&(j-1))
{
dp[S]=max(dp[S],dp[S^j]+dp[j]);
}
}
复杂度证明:
枚举所有集合的子集。
∑ i = 0 n ( n i ) 2 i = 3 n \sum_{i=0}^{n}\binom{n}{i}2^{i}= 3^{n} ∑i=0n(in)2i=3n
也就是o(3n)。
例题(牛客练习赛49B):
问题简述:就是这样
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
int p[20];
int plan[1<<20];
int dp[1<<20];
int num[1<<20];
int gt(int x)
{
if(x==0)return 0;
if(num[x])return num[x];
int ans=0;
int t=x;
while(t!=0)
{
ans++;
t-=t&-t;
}
return num[x]=ans;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int sum=0;
for(int i=0;i<n;i++)scanf("%d",&p[i]),sum+=p[i];
sort(p,p+n,greater<int>());
for(int i=1;i<=m;i++)
{
int tn;
scanf("%d",&tn);
int x=0;
for(int j=1;j<=tn;j++)
{
int w;
scanf("%d",&w);
x|=1<<w-1;
}
plan[x]=1;
}
dp[0]=0;
for(int i=1;i<1<<n;i++)
{
for(int j=i&(i-1);1;j=i&(j-1))
{
int e=0;
if(plan[i^j])e=p[gt(i)-1];
else e=0;
dp[i]=max(dp[i],dp[j]+e);
if(j==0)break;
}
}
printf("%d\n",sum-dp[(1<<n)-1]);
return 0;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43785386/article/details/103946695
边栏推荐
- 使用el-popconfirm和el-backtop不生效
- Discussion on frame construction and technology selection of short video platform
- PyTorch 11. Regularization
- PyTorch 19. Differences and relations of similar operations in pytorch
- 技术小白的第一篇(表达自己)
- 如何将进程绑定到指定的CPU上
- Error in multi machine and multi card training
- Typora语法详解(一)
- 免费开源充电桩物联网云平台
- 可视化常见问题解决方案(九)背景颜色问题
猜你喜欢
Int8 quantification and inference of onnx model using TRT
使用compressorjs压缩图片,优化功能,压缩所有格式的图片
Solution of emergency communication system for major security incidents
Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
自定义classloader并实现热部署-使用loadClass
可视化常见绘图(四)柱状图
Are realrange and einsum really elegant
可视化之路(十)分割画布函数详解
随机推荐
可视化常见问题解决方案(八)数学公式
Hanlp分词器(通过spark)
小程序换行符\n失效问题解决-日常踩坑
可视化常见绘图(三)面积图
Tensorflow安装后ImportError: DLL load failed: 找不到指定的模块,且国内安装缓慢
Jiangning hospital DMR system solution
Are realrange and einsum really elegant
Solution of wireless intercom system in Commercial Plaza
javscript获取文件真实后缀名
江宁医院DMR系统解决方案
直观理解熵
可视化常见问题解决方案(八)共享绘图区域问题解决方案
null和undefined的区别
小程序wx.previewMedia相关问题解决-日常踩坑
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
Source Insight 4.0常见问题
嵌入式相关面经(一)
PyTorch 17. GPU concurrency
remote: Support for password authentication was removed on August 13, 2021.
hql求一个范围内最大值