BZOJ 1079: [SCOI2008]着色方案(巧妙的dp)
题意
有\(n\)个木块排成一行,从左到右依次编号为\(1\)~\(n\)。你有\(k\)种颜色的油漆,其中第\(i\)种颜色的油漆足够涂\(c_i\)个木块。所有油漆刚好足够涂满所有木块,即\(\sum\limits _{i=1}^{k}c_i=n\)。统计任意两个相邻木块颜色不同的着色方案。(\(1 \le k \le 15\) ,\(1\le c_i \le 5\))
题解
特别巧妙的dp!一开始容易想到用\({c_i}^k\)时间复杂度做法QAQ,并没有什么用。
但是可以启发我们也许可以用\(k^{c_i}\)算法去解决问题。然而我还是不会。。
我就看了一下别人的博客2333 发现dp很巧妙
我们可以存储剩余能涂\(q\)个木块的油漆还剩多少种。这样时空复杂度就都降到\(k^{c_i}\)了。
所以就有dp[a][b][c][d][e]
来记录答案(a,b,c,d,e
分别表示1,2,3,4,5
的种数),所以就有
dp[a][b][c][d][e] = dp[a - 1][b][c][d][e] * a + dp[a + 1][b - 1][c][d][e] * b + dp[a][b + 1][c - 1][d][e] * c + dp[a][b][c + 1][d - 1][e] * d + dp[a][b][c][d + 1][e - 1] * e;
(之间的+1,-1
就是前面一种颜料从能涂q
块,变成q-1
了)
但这并不符合题目要求(不然一个组合数就结束了),所以我们多记一个状态last
表示上一次是用能涂last
次的油漆涂的,如果这次我们用last - 1
的话,就有一种颜料重复了,所以就要减去一种的贡献。
这样就基本做完了,但dp顺序有点麻烦,所以就上记忆化吧,十分简短易写,强力安利!
具体dp方程见程序吧。。不想写了QAQ
代码
/**************************************************************
Problem: 1079
User: zjp_shadow
Language: C++
Result: Accepted
Time:752 ms
Memory:67848 kb
****************************************************************/
#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define Fordown(i, r, l) for(register int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;
bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}
inline int read() {
int x = 0, fh = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar() ) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar() ) x = (x<<1) + (x<<3) + (ch ^ '0');
return x * fh;
}
void File () {
#ifdef zjp_shadow
freopen ("P1079.in", "r", stdin);
freopen ("P1079.out", "w", stdout);
#endif
}
const int N = 17, Mod = 1e9 + 7;
typedef long long ll;
ll dp[N][N][N][N][N][6];
ll Dp(int a, int b, int c, int d, int e, int last) {
if ((a | b | c | d | e) == 0) return 1;
ll &res = dp[a][b][c][d][e][last];
if (~res) return res; res = 0;
if (a) res += Dp(a - 1, b, c, d, e, 1) * (a - (last == 2) );
if (b) res += Dp(a + 1, b - 1, c, d, e, 2) * (b - (last == 3) );
if (c) res += Dp(a, b + 1, c - 1, d, e, 3) * (c - (last == 4) );
if (d) res += Dp(a, b, c + 1, d - 1, e, 4) * (d - (last == 5) );
if (e) res += Dp(a, b, c, d + 1, e - 1, 5) * e;
res %= Mod;
return res;
}
int main () {
File();
int n = read(), a[6] = {0};
For (i, 1, n) ++ a[read()];
Set(dp, -1);
printf ("%lld\n", Dp(a[1], a[2], a[3], a[4], a[5], 0) );
return 0;
}
BZOJ 1079: [SCOI2008]着色方案(巧妙的dp)的更多相关文章
- bzoj 1079: [SCOI2008]着色方案 DP
1079: [SCOI2008]着色方案 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 803 Solved: 512[Submit][Status ...
- BZOJ 1079: [SCOI2008]着色方案 记忆化搜索
1079: [SCOI2008]着色方案 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- BZOJ 1079 [SCOI2008]着色方案
http://www.lydsy.com/JudgeOnline/problem.php?id=1079 思路:如果把每种油漆看成一种状态,O(5^15)不行 DP[a][b][c][d][e][f] ...
- bzoj 1079: [SCOI2008]着色方案【记忆化搜索】
本来打算把每个颜色剩下的压起来存map来记忆化,写一半发现自己zz了 考虑当前都能涂x次的油漆本质是一样的. 直接存五个变量分别是剩下12345个格子的油漆数,然后直接开数组把这个和步数存起来,记忆化 ...
- 【BZOJ】1079: [SCOI2008]着色方案(dp+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1079 只能想到5^15的做法...........................果然我太弱. 其实 ...
- BZOJ1079 [SCOI2008]着色方案[组合计数DP]
$有a_{1}个1,a_{2}个2,...,a_{n}个n(n<=15,a_{n}<=5),求排成一列相邻位不相同的方案数.$ 关于这题的教训记录: 学会对于复杂的影响分开计,善于发现整体 ...
- 1079: [SCOI2008]着色方案
链接 思路 首先是dp,如果直接用每个种颜色的剩余个数做状态的话,复杂度为5^15. 由于c<=5,所以用剩余数量的颜色的种类数做状态:f[a][b][c][d][e][last]表示剩余数量为 ...
- bzoj1079: [SCOI2008]着色方案
ci<=5直接想到的就是5维dp了...dp方程YY起来很好玩...写成记忆化搜索比较容易 #include<cstdio> #include<cstring> #inc ...
- [SCOI2008]着色方案
1079: [SCOI2008]着色方案 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2228 Solved: 1353[Submit][Stat ...
随机推荐
- springIOC、AOP的一些注解
springIOC.AOP的一些注解(使用这些注解之前要导入spring框架的一些依赖): 1.注入IOC容器 @Compontent:使用注解的方式添加到ioc容器需要在配置文件 ...
- IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子
很久之前写了一篇SSH搭建例子,由于工作原因已经转到SpringMVC+Mybatis,就以之前SSH实现简单登陆的例子,总结看看SpringMVC+Mybatis怎么实现. Spring一开始是轻量 ...
- Sql server 卸载方法
sql server不正确卸载时,重新安装会失败,会提示各种错误:如数据库实例已存在等... 下面是我摸索总结出来的卸载方法,以及重装失败后的处理方法: 卸载方法: 注意:SQL Server 200 ...
- git命令行工作的正确姿势
git命令行创建并提交新分支到mater分支的常规步骤 git branch new_branch git status 查看修改的文件 git add changed_files git commi ...
- httpd的三种模式比较
查看你的httpd使用了哪种模式: /usr/local/apache2/bin/httpd -V |grep 'Server MPM' 使用哪种模式,需要在编译的时候指定 --with-mpm=pr ...
- Oracle数据库中SCOTT用户下的默认表
①EMP(雇员表): ②DEPT(部门表): ③BONUS(奖金表): ④SALGRADE(工资等级表):
- jsz中的作用域与上下文
var x=10; function fun() { console.log(x);//10 } function demo(f) { if(f instanceof Function){ fun() ...
- BUNOJ 1011
字符串处理的题.原题链接 AC代码: #include<cstring> #include<cstdio> #include<string> #include< ...
- 在SpringBoot中存放session到Redis
前言 今天你们将再一次领略到SpringBoot的开发到底有多快,以及SpringBoot的思想(默认配置) 我们将使用redis存放用户的session,用户session存放策略有很多,有存放到内 ...
- yaf代码生成工具的使用
具体步骤如下: 1.下载php-yaf源码: git clone https://github.com/laruence/php-yaf/ 2.运行代码生成工具: /Users/helloxiaozh ...