题意:给一个的格子图,有 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暴力)的更多相关文章

  1. 「状压DP」「暴力搜索」排列perm

    「状压DP」「暴力搜索」排列 题目描述: 题目描述 给一个数字串 s 和正整数 d, 统计 sss 有多少种不同的排列能被 d 整除(可以有前导 0).例如 123434 有 90 种排列能被 2 整 ...

  2. UVALive 6912 Prime Switch 状压DP

    Prime Switch 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  3. hihocoder #1608 : Jerry的奶酪(状压dp)

    题目链接:http://hihocoder.com/problemset/problem/1608 题解:就是一道简单的状压dp由于dfs过程中只需要几个点之间的转移所以只要预处理一下几个点就行. # ...

  4. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  5. 状压DP uvalive 6560

    // 状压DP uvalive 6560 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 // 思路: // dp[i][j]:第i行j状态下的值 ...

  6. UVAlive 6560 - The Urge to Merge(状压dp)

    LA 6560 - The Urge to Merge option=com_onlinejudge&Itemid=8&page=show_problem&problem=45 ...

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

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

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

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

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

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

随机推荐

  1. csv,txt,excel文件之间的转换,perl脚本

    最近接触一些需要csv,txt,excel文件之间的转换,根据一些网上搜索加上自己的改动,实现自己想要的结果为主要目的,代码的出处已经找不到了,还请见谅,以下主要是针对csv&excel 和t ...

  2. poj 3790 Recursively Palindromic Partitions (递推)

    题目 题意:求输入的数字的递归回文. 思路:答案等于这个数字一半之前的所有的 之和. #include <iostream> #include <cstdio> #includ ...

  3. 关于<img>标签与文字垂直居中

    要让左边的图片与后面的文字居中,如下效果 HTML代码: <img class="iconCls" alt="最新客户端" src="${bas ...

  4. Apache Struts2 s2-020补丁安全绕过漏洞

    CNVD-ID CNVD-2014-01552 发布时间 2014-03-11 危害级别 高 影响产品 Apache struts 2.0.0-2.3.16 BUGTRAQ ID 65999 CVE ...

  5. poj 1986 Distance Queries(LCA:倍增/离线)

    计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...

  6. shell -Z- d等等代表

    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 [ -a FILE ]   ...

  7. <四>面向对象分析之UML核心元素之用例

    一:基本概念        --->用例定义了一组用例实例,其中每个实例都是系统所执行一系列操作,这些操作生成特定主角可以观测的值.        --->所谓用例,就是一件事情,要完成这 ...

  8. oracle归档日志增长过快处理方法

    oracle归档日志一般由dml语句产生,所以增加太快应该是dml太频繁 首先查询以下每天的归档产生的情况: SELECT TRUNC(FIRST_TIME) "TIME", SU ...

  9. 基于Fragment实现Tab的切换,滑出侧边栏

    最近在学习Fragment(碎片)这是android3.0以后提出的概念,很多pad上面的设置部分都是通过Fragment来实现的,先看看具体的效果吧(图一)  (图二) (图三)第一章图片是初始时的 ...

  10. 36、Android Bitmap 全面解析

    Android Bitmap 全面解析(一)加载大尺寸图片 http://www.eoeandroid.com/thread-331669-1-1.html Android Bitmap 全面解析(二 ...