状态压缩·一

题目传送:#1044 : 状态压缩·一

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std; int n, m, q;
int w[1005]; int dp[1005][1030];//dp[i][j]表示选到第i个位置时j状态能够取得的最大值
int cnt[1030];//代表每一个数的位数上的1的个数 int main() {
cnt[0] = 0, cnt[1] = 1;
for(int i = 2; i < 1030; i ++) cnt[i] = cnt[i >> 1] + cnt[i & 1]; scanf("%d %d %d", &n, &m, &q);
for(int i = 1; i <= n; i ++) {
scanf("%d", &w[i]);
} int ans = 0;
int d = 1 << m;
for(int i = 1; i <= n; i ++) {
for(int j = 0; j < (1 << m); j ++) {
if(cnt[j] <= q) dp[i][j] = max(dp[i-1][j >> 1], dp[i-1][(j >> 1) + (1 << (m - 1))]) + (j & 1) * w[i];
ans = max(ans, dp[i][j]);
}
} printf("%d\n", ans);
return 0;
}

Hackers’ Crackdown

题目传送:UVA - 11825 - Hackers’ Crackdown

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std; const int maxn = (1 << 16) + 5;
int dp[maxn];//dp[i]表示子集i最多能够分成多少组
int cover[maxn];//cover[i]表示若干集合(i中所表示的集合)的并集 int n, m;
int P[25];//P[i]表示与i相连的数的集合(包含i) int main() {
int cas = 1;
while(scanf("%d", &n) != EOF) {
if(n == 0) break;
for(int i = 0; i < n; i ++) {
scanf("%d", &m);
P[i] = 1 << i;
int t;
while(m --) {
scanf("%d", &t);
P[i] |= (1 << t);//并入集合i
}
}
for(int i = 0; i < maxn; i ++) {
cover[i] = 0;
for(int j = 0; j < n; j ++) {//if推断j是否在i中,是得话就并入
if(i & (1 << j)) cover[i] |= P[j];
}
}
dp[0] = 0;
int tot = (1 << n) - 1;//全集总数
for(int i = 1; i <= tot; i ++) {//依次枚举全集,由于要先算出前一状态才干推出后一状态
dp[i] = 0;
for(int j = i; j; j = (j - 1) & i) {//枚举子集的技巧,重点!
if(cover[j] == tot) {//i的子集j等于全集,则运行状态转移
dp[i] = max(dp[i], dp[i ^ j] + 1);
}
}
}
printf("Case %d: %d\n", cas ++, dp[tot]);
}
return 0;
}

Sharing Chocolate

题目传送:UVALive - 4794 - Sharing Chocolate

WF2010的题。

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std; const int maxn = 16;
int d[1 << maxn][105];
int vis[1 << maxn][105];
int sum[1 << maxn];
int a[maxn];
int n; int bitcount(int x) {
return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
} int dp(int s, int x) {//每次递归找出集合为s。宽为x的巧克力能否够满足要求
if(vis[s][x]) return d[s][x];
vis[s][x] = 1;
int& ans = d[s][x];
if(bitcount(s) == 1) return ans = 1;//此时为边界。即仅仅有一块巧克力的情况,肯定是满足的
int y = sum[s] / x;//还有一个边长能够依据这个求得
for(int s0 = (s - 1) & s; s0; s0 = (s0 - 1) & s) {//枚举子集
int s1 = s - s0;
if(sum[s0] % x == 0 && dp(s0, min(x, sum[s0] / x)) && dp(s1, min(x, sum[s1] / x))) return ans = 1;//竖着切(这里假定宽x是竖着的)
if(sum[s0] % y == 0 && dp(s0, min(y, sum[s0] / y)) && dp(s1, min(y, sum[s1] / y))) return ans = 1;//横着切
}
return ans = 0;
} int main() {
int cas = 1, x, y;
while(scanf("%d", &n) != EOF) {
if(n == 0) break;
scanf("%d %d", &x, &y);
for(int i = 0; i < n; i ++) scanf("%d", &a[i]); //计算每一个子集的元素的和
memset(sum, 0, sizeof(sum));
for(int s = 0; s < (1 << n); s ++) {
for(int i = 0; i < n; i ++) if(s & (1 << i)) sum[s] += a[i];
} memset(vis, 0, sizeof(vis));
int ALL = (1 << n) - 1;
int ans;
if(sum[ALL] != x * y) ans = 0;
else ans = dp(ALL, min(x, y));
printf("Case %d: %s\n", cas ++, ans ? "Yes" : "No");
}
return 0;
}

状压DP问题的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  3. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  4. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  5. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  6. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  7. HDU 1074 Doing Homework (状压dp)

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

  8. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  9. 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

    [BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...

  10. 【BZOJ1087】 [SCOI2005]互不侵犯King 状压DP

    经典状压DP. f[i][j][k]=sum(f[i-1][j-cnt[k]][k]); cnt[i]放置情况为i时的国王数量 前I行放置情况为k时国王数量为J #include <iostre ...

随机推荐

  1. hibernate 批量抓取

    使用场景: 是查询出来一个集合,然后又查询每个集合对象中的集合.使用set标签中的batch-size属性实现. 数据库中只有5个区道信息: 设置batch-size=”5”,执行的查询语句如下: 而 ...

  2. 正则表达式,匹配查找函数(preg_match_all)flags参数对比

    格式: int preg_match_all ( string pattern, string subject, array matches [, int flags] ) 参数 flags 选项有以 ...

  3. 解决android的键盘弹出时,html页面的高度被压缩

    如果元素的高度是用100%表示,那么,安卓的键盘弹出时,高度会发生变化,导致布局混乱,所以最好给高度设置像素高度 $("html,body").height(window.inne ...

  4. 判断Exe(DLL)和符号文件是否匹配---验证模块和符号文件是否匹配的工具和方法

    当我们进行程序调试时,有时调试器会直接告诉你符号文件不对,或则显示出的调用栈不对,当你怀疑符号文件不匹配时,如何确定呢? 如果是用windbg调试,请用 !chksym 模块名比如,匹配的时候  不匹 ...

  5. Vue-cli 3.0自定义脚手架

    一.进入项目地址 https://github.com/vuejs/vue-cli ,选择 docs目录查看具体安装流程. 中文文档:https://cli.vuejs.org/zh 可以看到我电脑上 ...

  6. Ztree勾选节点后取消勾选其父子节点

    前言: Ztree官方给的API可以设置勾选一个节点的同时勾选子节点或者父节点,也可以设置不影响父子节点,即将chkboxType设置为{"Y":"",&quo ...

  7. Python中关于使用正则表达式相关的部分笔记

    一点点自己记的笔记,如果各位朋友看不懂,可以在评论区留言,会尽可能快的回复. 所有的知识点全部贴在代码上了,注释也写了. 建议大伙把代码拷到自己的机器上,运行,查看结果,然后,结合注释,再自己稍稍理解 ...

  8. 集训第六周 M题

    Description   During the early stages of the Manhattan Project, the dangers of the new radioctive ma ...

  9. JS数组添加元素的三种方式

    1.push() 结尾添加 数组.push(元素) 参数 描述 newelement1 必需.要添加到数组的第一个元素. newelement2 可选.要添加到数组的第二个元素. newelement ...

  10. windows10开启内置ubuntu系统,使用xshell连接

    windows安装配置ubuntu系统内置子系统 官方文档:https://docs.microsoft.com/zh-cn/windows/wsl/about https://www.jianshu ...