cf 398B. Painting The Wall【期望dp】

传送门:http://codeforces.com/problemset/problem/398/B

Description:

User ainta decided to paint a wall. The wall consists of
n2 tiles, that are arranged in an
n?×?n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.

  1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2.
  2. User ainta choose any tile on the wall with uniform probability.
  3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it.
  4. Then he takes a rest for one minute even if he doesn‘t paint the tile. And then ainta goes to step 1.

However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes
no time at all.

中文:涂墙壁游戏,要求每row每colum都至少一瓦被paint。

Analyse:

期望dp:从终点状态,往前面推;

dp(i,j)=(dp(i,j)+1)*p1+(dp(i-1,j)+1)*p2+(dp(i,j-1)+1)*p3+(dp(i-1,j-1)+1)*p4;

然后移项,dp(i,j)=.........详见CODE。=_=|

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=2007;

set<int> vr,vc;
double dp[N][N],p;

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        vr.clear(),vc.clear();
        p=1.0/n/n;
        int r,c;
        for(int i=0;i<m;i++){
            scanf("%d%d",&r,&c);
            if(!vr.count(r)) vr.insert(r);
            if(!vc.count(c)) vc.insert(c);
        }

        r=vr.size();
        c=vc.size();
        dp[0][0]=0;
        for(int i=0;i<=n-r;i++){
            for(int j=0;j<=n-c;j++){
                double tmp=0;
                if(i==0&&j==0) continue;
                tmp+=p*(n-i)*(n-j);
                dp[i][j]=1-tmp;
                if(i>0) tmp+=(dp[i-1][j]+1)*p*(n-j)*i;
                if(j>0) tmp+=(dp[i][j-1]+1)*p*(n-i)*j;
                if(i>0&&j>0) tmp+=(dp[i-1][j-1]+1)*p*i*j;
                dp[i][j]=tmp/dp[i][j];

            }
        }

        printf("%.10f\n",dp[n-r][n-c]);
    }
    return 0;
}
时间: 2024-09-28 12:46:55

cf 398B. Painting The Wall【期望dp】的相关文章

Codeforces Round #233 (Div. 2)D. Painting The Wall 概率DP

                                                                               D. Painting The Wall User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not

[Codefoeces398B]Painting The Wall(概率DP)

题目大意:一个$n\times n$的棋盘,其中有$m$个格子已经被染色,执行一次染色操作(无论选择的格子是否已被染色)消耗一个单位时间,染色时选中每个格子的概率均等,求使每一行.每一列都存在被染色的格子的期望用时. 传送门 显然,被染色的砖的位置对解题是没有影响的,我们可以将已染色砖所在的行和列移动到右下角,问题就转化到了在更小棋盘中的新问题. 在任一时刻,棋盘内的状态如下: 其中绿色区域为当前问题的棋盘,选中对行和列都有贡献: 选中黄色对行或列有贡献: 选中红色没有贡献: 设$f[i][j]

[Codeforces 464D]World of Darkraft(期望DP)

[Codeforces 464D]World of Darkraft(期望DP) 题面 游戏中有k种装备,每种装备初始时都是等级1.zyd每打一只怪,就会随机爆出一件装备.掉落和更新装备方式如下: 假设这种装备当前等级为t,那么系统会在[1,t+1]中等概率随机出该装备的等级.爆出装备后,会装备上身上的和爆出的装备之间等级更高的那件,并卖掉等级更低的装备.其中等级为i的装备价格为i金币. 求打了n只怪后获得金币的期望值,精确到\(10^{-9}\). \(n,k\leq 10^5\) 分析 首先

【bzoj4872】[Shoi2017]分手是祝愿 数论+期望dp

题目描述 Zeit und Raum trennen dich und mich. 时空将你我分开. B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态,下标为从 1 到 n 的正整数.每个灯有两个状态亮和灭,我们用 1 来表示这个灯是亮的,用 0 表示这个灯是灭的,游戏的目标是使所有灯都灭掉.但是当操作第 i 个开关时,所有编号为 i 的约数(包括 1 和 i)的灯的状态都会被改变,即从亮变成灭,或者是从灭变成亮.B 君发现这个游戏很难,于是想到了这样的一个

HDOJ 1145 So you want to be a 2n-aire? 期望DP

期望DP So you want to be a 2n-aire? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 267    Accepted Submission(s): 197 Problem Description The player starts with a prize of $1, and is asked a seq

HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由于得到每张卡片的状态不知道,所以用状态压缩,dp[i] 表示这个状态时,要全部收齐卡片的期望. 由于有可能是什么也没有,所以我们要特殊判断一下.然后就和剩下的就简单了. 另一个方法就是状态压缩+容斥,同样每个状态表示收集的状态,由于每张卡都是独立,所以,每个卡片的期望就是1.0/p,然后要做的就是要去重,既然

Topcoder SRM656div1 250 ( 期望DP )

Problem Statement    Charlie has N pancakes. He wants to serve some of them for breakfast. We will number the pancakes 0 through N-1. For each i, pancake i has width i+1 and deliciousness d[i].Charlie chooses the pancakes he is going to serve using t

期望dp 知识点

求期望dp有两种类型 1.概率dp 2.高斯消元 相关知识点可以看这里  一篇很好的文章  http://kicd.blog.163.com/blog/static/126961911200910168335852/ http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710606.html 高斯消元  http://wenku.baidu.com/link?url=Q8ES7wreJk3et-VrHtp6CVNuyqX18YdB3c841-o

string (KMP+期望DP)

Time Limit: 1000 ms   Memory Limit: 256 MB Description  给定一个由且仅由字符 'H' , 'T' 构成的字符串$S$. 给定一个最初为空的字符串$T$ , 每次随机地在$T$的末尾添加 'H' 或者 'T' . 问当$S$为$T$的后缀时, 在末尾添加字符的期望次数. Input 输入只有一行, 一个字符串$S$. Output 输出只有一行, 一个数表示答案. 为了防止运算越界, 你只用将答案对$10^9+7$取模. Sample Inp