[POJ1038]状压DP
题意:给一个n*m的区域,里面有一些障碍物,往里面放2*3和3*2的矩形,矩形之间不能重叠,不能覆盖到障碍物,求能放置的最大个数。(n<=150,m<=10)
思路:看到m=10就应该往状压dp方面想了。由于有3*2的矩形,所以需要记录2行的状态,粗略估计状态数高达150*2^20=1.5*1e8,这么多状态必然超时,注意到如果(i-1,j)为0了,无论(i,j)为1或0,(i,j)都不能放矩形,于是知道有很多无用的或者说不合法的状态,两行的状态用m位3进制数表示同样能实现转移。由于3进制数操作起来麻烦,不妨用4进制代替3进制,从当前状态向后递推,新状态存在vector里,使用的时候先排序,然后跳过重复或不够优的状态来向后扩展。经测试,极限数据下,vector里有40000多个状态,有效状态只有1700多个,一下子降了一个数量级,简直逆天...对于这种存在大量无效状态的dp ,用vector+向后递推+排序去重有奇效。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <vector> #include <ctime> #include <deque> #include <queue> #include <algorithm> using namespace std; void readInt(){} void RI( int &X){ scanf ( "%d" ,&X);} template < typename ...R> void RI( int &f,R&...r){RI(f);RI(r...);} void RIA( int *p, int *q){ int d=p<q?1:-1; while (p!=q){ scanf ( "%d" ,p);p+=d;}} void print(){cout<<endl;} template < typename T> void print( const T t){cout<<t<<endl;} template < typename F, typename ...R> void print( const F f, const R...r){cout<<f<< ", " ;print(r...);} template < typename T> void print(T*p, T*q){ int d=p<q?1:-1; while (p!=q){cout<<*p<< ", " ;p+=d;}cout<<endl;} template < typename T> bool umax(T &a, const T &b) { return a >= b? false : (a = b, true ); } typedef pair< int , int > pii; #define pb push_back #define mp make_pair #define X first #define Y second #define all(a) (a).begin(), (a).end() vector<pii> dp[2]; bool cmp( const pii &a, const pii &b) { return a.X < b.X || a.X == b.X && a.Y > b.Y; } int sta[157], now, row, ttl, n, m, k; pii s; bool chk( const int &s, const int &p) { return s & (1 << p); } void dfs( int col, int S, int V) { if (col == m) { dp[now ^ 1].pb(mp(S, V)); return ; } dfs(col + 1, S, V); int low = S & ttl, high = S >> m, r = low | high; if (col + 2 < m && !chk(r, col) && !chk(r, col + 1) && !chk(r, col + 2)) { int h = (1 << col) ^ (1 << (col + 1)) ^ (1 << (col + 2)); dfs(col + 3, (high ^ h) << m | (low ^ h), V + 1); } r |= s.X >> m; if (col + 1 < m && !chk(r, col) && !chk(r, col + 1)) { int h = (1 << col) ^ (1 << (col + 1)); dfs(col + 2, (high ^ h) << m | (low ^ h), V + 1); } } int main() { #ifndef ONLINE_JUDGE freopen ( "in.txt" , "r" , stdin); #endif // ONLINE_JUDGE int T; cin >> T; while (T --) { RI(n, m, k); memset (sta, 0, sizeof (sta)); for ( int i = 0; i < k; i ++) { int x, y; RI(x, y); sta[x] ^= 1 << (y - 1); } dp[0].clear(); dp[1].clear(); now = 0; ttl = (1 << m) - 1; dp[0].pb(mp(ttl << m | sta[1], 0)); for ( int i = 1; i < n; i ++) { dp[now ^ 1].clear(); sort(all(dp[now]), cmp); int sz = dp[now].size(); for ( int j = 0; j < sz; j ++) { s = dp[now][j]; if (!j || s.X != dp[now][j - 1].X) dfs(0, (s.X << m | sta[i + 1]) & (ttl << m | ttl), s.Y); } now ^= 1; } int ans = 0; for ( int i = 0; i < dp[now].size(); i ++) { umax(ans, dp[now][i].Y); } cout << ans << endl; } return 0; } |
[POJ1038]状压DP的更多相关文章
- POJ1038 Bugs Integrated, Inc 状压DP+优化
(1) 最简单的4^10*N的枚举(理论上20%) (2) 优化优化200^3*N的枚举(理论上至少50%) (3) Dfs优化状压dp O(我不知道,反正过不了,需要再优化)(理论上80%) (4) ...
- poj1038 Bugs Integrated,Inc. (状压dp)
题意:N*M的矩阵,矩阵中有一些坏格子,要在好格子里铺2*3或3*2的地砖,问最多能铺多少个. 我的方法好像和网上流传的方法不太一样...不管了.... 由数据范围很容易想到状压dp 我们设某个状态的 ...
- poj2411 Mondriaan's Dream[简单状压dp]
$11*11$格子板上铺$1*2$地砖方案.以前做过?权当复习算了,毕竟以前学都是浅尝辄止的..常规题,注意两个条件:上一行铺竖着的则这一行同一位一定要铺上竖的,这一行单独铺横的要求枚举集合中出现连续 ...
- POJ 1038 Bugs Integrated Inc (复杂的状压DP)
$ POJ~1038~~\times Bugs~Integrated~Inc: $ (复杂的状压DP) $ solution: $ 很纠结的一道题目,写了大半天,就想练练手,结果这手生的.其实根据之前 ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
随机推荐
- Redis的三大问题
一般我们对缓存读操作的时候有这么一个固定的套路: 如果我们的数据在缓存里边有,那么就直接取缓存的. 如果缓存里没有我们想要的数据,我们会先去查询数据库,然后将数据库查出来的数据写到缓存中. 最后将数据 ...
- 01、Hibernate安装配置
1.查看你的Eclipse的版本:Help | About Eclipse Version: 2018-12 (4.10.0) 2.HibernateTools的下载地址为:http:// ...
- 图数据库的内部结构 (NEO4j)
What “Graph First” Means for Native Graph Technology Neo4j是一个具有原生处理(native processing)功能和原生图存储(nativ ...
- SpringCloud-Config 配置中心
概述 分布式系统面临的问题 微服务意味着要将单体应用中的业务拆分成一个个的子服务,这些服务都需要必要的配置信息才能运行,如果有上百个微服务,上百个配置文件,管理起来是非常困难的,这时候,一套集中式的. ...
- 百度paddlepaddle学习体会
一个偶然从微信公众号中刷到了<python小白逆袭A1大神>的文章,让我不经意的邂逅了飞桨(paddlepaddle),通过加入飞桨训练营一周的学习.实践,对飞桨有了很多的了解(飞桨官网: ...
- /uesr/local/hadoop/tmp/mapred有锁
原因: /usr/local/hadoop/tmp/mapred 有锁 解决:修改改文件的权限 在终端输入: cd /usr/local/hadoop/tmp sudo chmod 777 map ...
- 源码安装nginx 方法二
yum 仓库不能用大写字母 [root@oldboy conf.d]# gzip * 压缩当前目录下的所有文件 gzip ./* gzip . gzip./ # 关闭防火墙和selinux [root ...
- Java9新特性系列(module&maven&starter)
上篇已经深入分析了Java9中的模块化,有读者又提到了module与starter是什么关系?本篇将进行分析. 首先先回顾下module与maven/gradle的关系: module与maven/g ...
- 开始导入第一个第三方库jieba
在做python的练习题,想看看运行结果. 谁知,有道题,不能识别jieba,原来要导入,因为是第三方库,照着书里面的导入方法,有三种,一种是用pip,在命令行里面安装,使用pip - p 可以查看p ...
- Codeforce 140C (贪心+优先队列)补题
C. New Year Snowmen time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...