codeforces D. Painting The Wall

http://codeforces.com/problemset/problem/399/D

题意:给出n和m,表示在一个n*n的平面上有n*n个方格,其中有m块已经涂色。现在随机选中一块进行涂色(如果已经涂色跳过,也消耗时间),消耗1个步骤。终止条件为每行每列都有至少有一块瓷砖被涂色。问说涂成满意的情况需要时间的期望。

思路:把整个方格分成四部分,如果选择左上角上的一块,那么行和列都将被涂上一个;右上角的话,行被涂上一个,列不变;左下角的话,行不变,列被涂上一个;右下角,行列都不变。

状态转移方程:dp[i][j]=(dp[i+1][j]*(n-i)*j+dp[i][j+1]*(n-j)*i+dp[i+1][j+1]*(n-i)*(n-j)+n*n)/(n*n-i*j);

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define LL __int64
 6 using namespace std;
 7
 8 int n,m;
 9 int nr[2010],nc[2010];
10 double dp[2010][2010];
11
12 int main()
13 {
14     scanf("%d%d",&n,&m);
15     int tr=0,tc=0;
16     for(int i=1; i<=m; i++)
17     {
18         int r,c;
19         scanf("%d%d",&r,&c);
20         if(!nr[r])
21         {
22             tr++;
23             nr[r]++;
24         }
25         if(!nc[c])
26         {
27             tc++;
28             nc[c]++;
29         }
30     }
31     dp[n][n]=0;
32     for(int i=n; i>=0; i--)
33     {
34         for(int j=n; j>=0; j--)
35         {
36             if(i!=n||j!=n)
37             dp[i][j]=(double)((n-i)*j*dp[i+1][j]+i*(n-j)*dp[i][j+1]+(n-i)*(n-j)*dp[i+1][j+1]+n*n)/(n*n-i*j);
38         }
39     }
40     printf("%.10lf\n",dp[tr][tc]);
41     return 0;
42 }

时间: 2024-08-07 10:08:58

codeforces D. Painting The Wall的相关文章

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

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 beautifull

Codeforces 282B. Painting Eggs

The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is c

(dp)CodeForces - 300D Painting Square

Vasily the bear has got a large square white table of n rows and n columns. The table has got a black border around this table.  The example of the initial table at n = 5. Vasily the bear wants to paint his square table in exactly k moves. Each move

codeforces 507B. Painting Pebbles 解题报告

题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色去填充所有存在的pebbles,使得任意两个piles,用颜色c填充的pebbles数量之差 <= 1.如果不填充某种颜色,就默认数量为0. 这样说还是比较难理解吧---以第三组数据为例: 5 43 2 4 3 5 YES1 2 31 31 2 3 41 3 41 1 2 3 4 第2个 pile

Codeforces 448C Painting Fence:分治

题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆. 每次你可以选择竖着刷或横着刷,但必须保证一次刷的地方不能间断. 问你至少要刷几次才能刷满. 题解: 首先有一个贪心结论: 对于当前要刷的一片区域,令minn为这片区域的最小高度. 如果选择横着刷,则至少要将区域底部的minn层刷完. 如图,至少要将下面两层刷完: 然后考虑如何分治: 对于当前的这

Codeforces 448C Painting Fence(分治法)

题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅栏,现在要给栅栏刷漆,刷子宽1,刷子可以刷任意长,每次只能横着刷或者竖着刷,问最少需要刷几次?解题思路:参考了这里(https://blog.csdn.net/qq_24451605/article/details/48492573)首先我们能够想到,如果横着刷,为了得到最优解,当前刷的位置的下面也

codeforces 448C Painting Fence

刷篱笆,只有横和竖, 竖着肯定最多是 n , 另一种 那么每一次先把最下面的那个刷掉,  刷掉之后 ,  继续把上面的  刷掉,, 每一次把  剩下的   再按横着或竖着  刷 就是分治了 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; #define inf 0x7f7f7f7f int a[6000]; int

!codeforces 399D Painting The Wall-概率dp

题意:有一个n*n的墙,现在小明来刷墙,如果每一行每一列都至少有一个格子刷过了就停止工作,否则每次随机选一个格子,如果刷过了就不刷如果没刷过就刷,然后休息一分钟,求停止工作时时间的数学期望(开始之前已经有m个格子刷过了) 分析:概率dp 状态:dp[i][j]表示还有i行j列没刷,则它能转移到的状态是dp[i][j],dp[i-1][j-1],dp[i][j-1],dp[i-1][j-1] 转移:dp[i][j]=dp[i][j]*(n-i)(n-j)/n^2+dp[i-1][j]*(i*(n-