状态压缩·一

题目传送:#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. Maven常用仓库地址以及手动添加jar包到仓库

    http://www.blogjava.net/fancydeepin 共有的仓库 http://repository.sonatype.org/content/groups/public/http: ...

  2. 07JavaScript数组与字符串对象

    JavaScript数组与字符串对象 5.1.1数组(Array)对象 <script> //声明一个数组并赋值; var arr = new Array("aa",& ...

  3. selenium+python自动化unittest之跳过用例skip

    前言 当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例 ...

  4. qrcode.js

    (function(r){r.fn.qrcode=function(h){var s;function u(a){this.mode=s;this.data=a}function o(a,c){thi ...

  5. 洛谷——P2055 [ZJOI2009]假期的宿舍

    P2055 [ZJOI2009]假期的宿舍 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 ...

  6. 关于C/C++的一些思考(3)

    操作符重载函数(Operator Overload Function)的基本概念: 目的是以与对待内置数据类型相同的方式对待用户自定义类型(程序执行速度会受到影响),限制是不能随意选择函数名和参数个数 ...

  7. DNS服务器原理简述、搭建主/从DNS服务器并实现智能解析

    1. TLD:Top Level Domain 顶级域名 组织域:.com, .net, .org, .gov, .edu, .mil 国家域:.iq, .tw, .hk, .jp, .cn, ... ...

  8. C语言的移位操作符及位运算

    C语言的移位操作符 位移位运算符是将数据看成二进制数,对其进行向左或向右移动若干位的运算.位移位运算符分为左移和右移两种,均为双目运算符.第一运算对象是移位对象,第二个运算对象是所移的二进制位数. 位 ...

  9. DFS template and summary

    最近一直在学习Deep Frist Search,也在leetcode上练习了不少题目.从最开始的懵懂,到现在遇到问题基本有了思路.依然清晰的记得今年2月份刚开始刷题的时做subsets的那个吃力劲, ...

  10. Redis事务【十二】

    一.概述: 和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基石.相信对有关系型数据 ...