UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)
题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法。
析:感觉像个DP,但是不会啊。。。就想暴力试试,反正数据量看起来不大才7,但是。。。TLE了,又换了一个暴力方法,2秒多过了,差点啊。
其实这是一个状压DP,dp[i][s]表示在第 i 列,在集合 s 中有方法数,那么怎么转移呢,这个还是挺简单的,就是判断第i+1列是不是比第 i 列都大于等于就ok了,
输入时先把行,转化成列,再计算,初始化就是第一列喽,假设什么组合都行。
代码如下:
暴力代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 450 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int w[10], h[10];
int ans = 0, cnt[10][10]; void dfs(int r, int c, int mx){
if(c > m){
++ans; return ;
}
int mm = Min(n, n-h[c]+r); for(int i = Max(mx+1, cnt[r][c-1]); i <= mm; ++i){
cnt[r][c] = i;
if(r == h[c]) dfs(1, c+1, 0);
else dfs(r+1, c, i);
}
} int main(){
int k;
while(scanf("%d", &k) == 1){
memset(h, 0, sizeof h);
m = 0;
for(int i = 1; i <= k; ++i){
int x;
scanf("%d", &x);
m = Max(m, x);
for(int j = 1; j <= x; ++j)
++h[j];
} scanf("%d", &n);
ans = 0;
memset(cnt, 0, sizeof cnt);
dfs(1, 1, 0);
printf("%d\n", ans);
}
return 0;
}
状压DP代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 8;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int a[10];
int dp[maxn][1<<maxn]; inline int lowbit(int x){
return x & (-x);
} inline int bitcount(int i){//返回 i 这个数二进制数中有几个1
int ans = 0;
while(i) ++ans, i -= lowbit(i);
return ans;
} inline bool judge(int j, int k){//判断第 j 列是不是小于等于第 k 列
int tj[10], tk[10];
int cntj = 0, cntk = 0;
for(int i = 0; i < m; ++i){
if(j>>i&1) tj[cntj++] = i;
if(k>>i&1) tk[cntk++] = i;
}
for(int i = 0; i < cntk; ++i)
if(tj[i] > tk[i]) return false;
return true;
} int main(){
while(scanf("%d", &n) == 1){
memset(a, 0, sizeof a);
int t = 0, x;
for(int i = 0; i < n; ++i){
scanf("%d", &x);
t = Max(t, x);
for(int j = 1; j <= x; ++j)
++a[j];
} memset(dp, 0, sizeof dp);
scanf("%d", &m);
int len = 1<<m;
for(int i = 0; i < len; ++i)//初始化第 1 列
if(bitcount(i) == a[1]) dp[1][i] = 1; for(int i = 1; i < t; ++i){
for(int j = 0; j < len; ++j){
if(bitcount(j) != a[i] || !dp[i][j]) continue; for(int k = 0; k < len; ++k)
if(bitcount(k) == a[i+1] && judge(j, k)) dp[i+1][k] += dp[i][j];
}
} int ans = 0;
for(int i = 0; i < len; ++i) ans += dp[t][i];
printf("%d\n", ans);
}
return 0;
}
UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)的更多相关文章
- 「状压DP」「暴力搜索」排列perm
「状压DP」「暴力搜索」排列 题目描述: 题目描述 给一个数字串 s 和正整数 d, 统计 sss 有多少种不同的排列能被 d 整除(可以有前导 0).例如 123434 有 90 种排列能被 2 整 ...
- UVALive 6912 Prime Switch 状压DP
Prime Switch 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- hihocoder #1608 : Jerry的奶酪(状压dp)
题目链接:http://hihocoder.com/problemset/problem/1608 题解:就是一道简单的状压dp由于dfs过程中只需要几个点之间的转移所以只要预处理一下几个点就行. # ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- 状压DP uvalive 6560
// 状压DP uvalive 6560 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 // 思路: // dp[i][j]:第i行j状态下的值 ...
- UVAlive 6560 - The Urge to Merge(状压dp)
LA 6560 - The Urge to Merge option=com_onlinejudge&Itemid=8&page=show_problem&problem=45 ...
- 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 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
随机推荐
- 基于XMPP的即时通信系统的建立(四)— 协议详解
Presence 在XMPP协议中,我们使用presence来获取用户是否已经上线以及是否可以通信的状态. 为了能够知道自己联系人的状态以及让联系人知道自己的状态,用户上线后需要订阅联系人的状态,联系 ...
- 对象不支持“attachEvent”属性或方法的解决办法
有些脚本在IE11下执行会报错误: 对象不支持“attachEvent”属性或方法 解决办法 解决办法:把attachEvent改为addEventListener即可
- Html5大文件断点续传
大文件分块 一般常用的web服务器都有对向服务器端提交数据有大小限制.超过一定大小文件服务器端将返回拒绝信息.当然,web服务器都提供了配置文件可能修改限制的大小.针对iis实现大文件的上传网上也 ...
- Run busybox httpd with php, sqlite
/*********************************************************************************** * Run busybox h ...
- <一>SQL优化1-4
第一条:去除在谓词列上编写的任何标量函数 --->在select 显示列上使用标量函数是可以的.但在where语句后的过滤条件部分对列使用函数,需要考虑.因为执行sql的引擎会因为 ...
- 【转】.. Android应用内存泄露分析、改善经验总结
原文网址:http://wetest.qq.com/lab/view/107.html?from=ads_test2_qqtips&sessionUserType=BFT.PARAMS.194 ...
- Android Message和obtainMessage的区别
类概述 定义一个包含任意类型的描述数据对象,此对象可以发送给Handler.对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作. 尽管Message的构造器是公开 ...
- View.VISIBLE、INVISIBLE、GONE的区别
android中UI应用的开发中经常会使用view.setVisibility()来设置控件的可见性,其中该函数有3个可选值,他们有着不同的含义: View.VISIBLE--->可见View. ...
- UVALive 4255 Guess
这题竟然是图论···orz 题意:给出一个整数序列a1,a2,--,可以得到如下矩阵 1 2 3 4 1 - + 0 + 2 + + + 3 - - 4 + &quo ...
- 在Ubuntu下安装Apache
在Ubuntu下安装软件其实非常方便,Ubuntu提供了apt-get工具,可以使用该工具直接下载安装软件. 在Linux里,系统最高权限账户为root账户,而默认登录的账户并非root账户,例如不具 ...