BZOJ 1079: [SCOI2008]着色方案 记忆化搜索

1079: [SCOI2008]着色方案

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1079

Description

有n个木块排成一行,从左到右依次编号为1~n。你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块。所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n。相邻两个木块涂相同色显得很难看,所以你希望统计任意两个相邻木块颜色不同的着色方案。

Under two situations the player could score one point.

⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won‘t get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.

There are three types of players.

Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it‘s difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.

There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.

The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

第一行为一个正整数k,第二行包含k个整数c1, c2, ... , ck。

Output

输出一个整数,即方案总数模1,000,000,007的结果。

Sample Input

3
1 2 3

Sample Output

10

HINT

题意

题解:

直接dp[a][b][c][d][e][f]表示上一个状态为f时,我可以涂1次的有a个,涂两次的有b个,涂三次的有c个,涂四次的有d个,涂五次的有e个的方案数

直接记忆化搜索转移就好了

代码

#include<iostream>
#include<stdio.h>
using namespace std;
int tmp[10];
int dp[16][16][16][16][16][7];
int vis[16][16][16][16][16][7];
#define mod 1000000007
long long dfs(int a,int b,int c,int d,int e,int n){
    if(vis[a][b][c][d][e][n])
        return dp[a][b][c][d][e][n];
    if(a+b+c+d+e==0)
        return dp[a][b][c][d][e][n]=1;
    long long ans=0;
    if(a)ans+=(a-(n==2))*dfs(a-1,b,c,d,e,1);
    if(b)ans+=(b-(n==3))*dfs(a+1,b-1,c,d,e,2);
    if(c)ans+=(c-(n==4))*dfs(a,b+1,c-1,d,e,3);
    if(d)ans+=(d-(n==5))*dfs(a,b,c+1,d-1,e,4);
    if(e)ans+=(e-(n==6))*dfs(a,b,c,d+1,e-1,5);
    vis[a][b][c][d][e][n]=1;
    ans%=mod;
    return dp[a][b][c][d][e][n]=ans;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int x;scanf("%d",&x);
        tmp[x]++;
    }
    printf("%d\n",dfs(tmp[1],tmp[2],tmp[3],tmp[4],tmp[5],0));
}
时间: 2024-10-05 04:27:45

BZOJ 1079: [SCOI2008]着色方案 记忆化搜索的相关文章

BZOJ 1079: [SCOI2008]着色方案(巧妙的dp)

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

BZOJ 1079: [SCOI2008]着色方案 DP

1079: [SCOI2008]着色方案 Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得很难看,所以你希望统计任意两个相邻木块颜色不同的着色方案. Input 第一行为一个正整数k,第二行包含k个整数c1, c2, ... , ck. Output 输出一个整数,即方案总数模1,000,000,007的结果. Sample Inp

bzoj1079 着色方案 记忆化搜索(dp)

题目传送门 题目大意: 有k种颜色,每个颜色ci可以涂个格子,要求相邻格子颜色不能一样,求方案数.ci<=5,k<=15. 思路: 题目里最重要的限制条件是相邻格子颜色不能相同,也就是当前格子只和上一个格子有关,那么对于还剩相同个数的颜色,如果都和上一个颜色不一样的话,那么这几种颜色都是一样的.如果某一种颜色和上一个颜色一样,那这个不算就可以了. 所以f[a][b][c][d][e][last]表示,还剩1次的颜色有a个,2两次颜色有b个,3次的颜色有c个,4次的颜色d个,5次的颜色e个,上一

bzoj 1079: [SCOI2008]着色方案

1 #include<cstdio> 2 #include<iostream> 3 #define M 1000000007 4 using namespace std; 5 int f[16][16][16][16][16][6],ma[16][16][16][16][16][6],a[6],k; 6 long long dp(int a1,int a2,int a3,int a4,int a5,int a6) 7 { 8 if(a1+a2+a3+a4+a5==0) 9 retu

BZOJ 3895 取石子 博弈论+记忆化搜索

题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都>1 那么我们定义操作数b为当前石子总数+当前堆数-1 若b为奇数,则先手必胜,否则后手必胜 证明: 若当前只有一堆,则正确性显然 否则: 若b为奇数,那么先手只需进行一次合成操作,此时操作数会-1,且仍不存在大小为1的堆 因此只需要证明b为偶数时先手必败即可 若先手选择了合成操作,那么操作数-1且不存在大小为1的堆,状态回到了b

bzoj1079: [SCOI2008]着色方案

ci<=5直接想到的就是5维dp了...dp方程YY起来很好玩...写成记忆化搜索比较容易 #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #

bzoj1079: [SCOI2008]着色方案(dp)

1079: [SCOI2008]着色方案 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2133  Solved: 1287[Submit][Status][Discuss] Description 有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得很难看,所以你希望统计任意两个相邻木块颜色不同的着色方案.

路径方案数_mod_SPFA_记忆化搜索_C++

本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本来是写个 DP 分分钟就 A 了,结果老师要我们写记忆化搜索(无奈脸) 算啦,随手一改又是一个标准的记忆化搜索(目测好像是记忆化搜索容易码一些,而且跑得快一些) 话说不取模也可以A,数据太水 很水的题吧,先 SPFA 跑一遍 2 的最短路,然后记忆化搜索统计方案 不难证明在加上最短路的限制条件后,图变成了一个 DAG 证明:首先有向是显然的,不可能存在两点 x,y,它们的最短路 d[x]>d[y] 又 d[x]<d[y] 若存在一

[BZOJ 1055] [HAOI2008] 玩具取名 【记忆化搜索】

题目链接:BZOJ - 1055 题目分析 这种类似区间 DP 的记忆化搜索都是很相近的,比如字符串压缩和字符串扩展都差不多. 都是将现在 Solve 的区间分成子区间,再求解子区间. 这道题 Solve(l, r, x) 求能否将 [l, r] 的区间还原成 x ,那么就将它分成两段,看是否能左段变成 p , 右段变成 q. (x 能变成 pq) 代码 #include <iostream> #include <cstdio> #include <cstdlib> #