Problem Statement

     You might have played the game called Memoria. In this game, there is a board consisting of N rows containing M cells each. Each of the cells has a symbol on its back. Each symbol occurs on exactly two cells on the board.

A move means turning a pair of cells one by one to see the symbols behind them. When the symbols differ, both of the cells are turned on their faces, thus hiding the symbols again. The player should remember the symbols. If the symbols on the backs of the turned cells coincide, the cells stay that way, i.e., don't turn back again. As soon as after some move all the cells on the board are turned (such that all the symbols are simultaneously visible), the game ends.

Manao has a perfect memory, so when he sees the symbol behind some cell, he remembers it forever. Manao is trying to finish the game in the least expected number of moves. Find the expected number of moves he will need to accomplish this.

Definition

    
Class: PerfectMemory
Method: getExpectation
Parameters: int, int
Returns: double
Method signature: double getExpectation(int N, int M)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Notes

- The board Manao plays on is generated as follows. The same set of (N * M) / 2 symbols is used for each generation. The board contents are chosen uniformly among all valid N x M boards.
- The returned value must have an absolute or relative error less than 1e-9.

Constraints

- N will be between 1 and 50, inclusive.
- M will be between 1 and 50, inclusive.
- N * M will be even.

Examples

0)  
    
1
2
Returns: 1.0
There are only two cells on the board, so the game always ends in one move.
1)  
    
2
2
Returns: 2.6666666666666665
There are four cells. The game may flow in two possible scenarios:

1) In the first move, Manao turns two cells with equal symbols. The game ends in two moves then and the probability of such a first move is 1/3.

2) In the first move, Manao turns two cells with different symbols. Then he finishes the game in three moves and the probability of such a first move is 2/3.

The overall expected number of moves is 1/3 * 2 + 2/3 * 3 = 8/3.

2)  
    
2
3
Returns: 4.333333333333334
 
3)  
    
4
4
Returns: 12.392984792984793
 

题意:你有n*m张卡片,总共n*m/2种,每种有两张。每次操作时你可以翻开两张,若一样,则它们不再翻回去;否则,这两张卡面会向你展示后再翻回去(不改变位置),假设你记忆力很好,能够记住牌的种类。求出至少进行多少次操作,可以使所有牌都同时正面朝上。注意,一次操作翻牌是有先后的,即你可以先翻一张牌,看过其种类后,再决定翻第二张牌。

题解:

假设翻到一样的牌,它们就会被消掉。dp[i][j]表示场上还有i+j张牌未被消掉,已经翻开过其中i张牌(即知道其种类),剩下的j张牌还未被翻开过。

每次操作一定会先翻一张未被翻过的牌,若其与i张牌中的一张对应,则第二张牌一定翻这张,把它们消掉。若是一张新的牌,则第二张牌一定也翻未被翻过的牌。

计算每种情况出现的概率与期望,以j为阶段、i为状态,进行动态规划。

代码:

 double dp[][];
class PerfectMemory
{
public:
double getExpectation(int N, int M)
{
//$CARETPOSITION$
int n=N*M; dp[][]=;
for(int j=;j<=n;j++)
for(int i=;i<=n;i++)
{
if((i==)and(j==))continue; dp[i][j]=;
if(i>j)continue; if(i+j>n)continue; if((j-i)%==)continue;
dp[i][j]=; double p,p2,a1;
p=i; p=p/j; p2=-p;
if((j>)and(i>))
{
dp[i][j]=dp[i][j]+p*(+dp[i-][j-]);
}
double b,c,d;
if(j>)
{
b=; b=b/(j-); c=j--i; c=c/(j-); d=-b-c;
a1=b*(+dp[i][j-])+c*(+dp[i+][j-])+d*(+dp[i][j-]);
dp[i][j]=dp[i][j]+p2*a1;
}
}
return dp[][n]; }
};

TopCoder[SRM513 DIV 1]:PerfectMemory(500)的更多相关文章

  1. TopCoder[SRM513 DIV 1]:Reflections(1000)

    Problem Statement      Manao is playing a new game called Reflections. The goal of the game is trans ...

  2. Topcoder SRM584 DIV 2 500

    #include <set> #include <iostream> #include <string> #include <vector> using ...

  3. topcoder srm 628 div2 250 500

    做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: #i ...

  4. SRM 719 Div 1 250 500

    250: 题目大意: 在一个N行无限大的网格图里,每经过一个格子都要付出一定的代价.同一行的每个格子代价相同. 给出起点和终点,求从起点到终点的付出的最少代价. 思路: 最优方案肯定是从起点沿竖直方向 ...

  5. Topcoder SRM583 DIV 2 250

    #include <string> #include <iostream> using namespace std; class SwappingDigits { public ...

  6. 【补解体报告】topcoder 634 DIV 2

    A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...

  7. Topcoder Srm627 DIV 2

    A,B:很水,注意边界,话说HACK都是这些原因. C: R[I][J]:表示反转I-J能改变冒泡排序的次数: DP方程:dp[i][k]=max(dp[j][k],dp[j][k-1]+dp[j][ ...

  8. Topcoder SRM548 Div 1

    1. KingdomAndTrees 给出n个数a[1..n],求一个数组b[1..n]满足b严格递增,且b[1]>=1. 定义代价为W = max{abs(a[i]-b[i])},求代价最小值 ...

  9. TopCoder SRM 675 Div1 Problem 500 LimitedMemorySeries1(分块)

    题意  给定一个长度不超过$5*10^{6}$的数列和不超过$100$个询问,每次询问这个数列第$k$小的数,返回所有询问的和 内存限制很小,小到不能存下这个数列.(数列以种子的形式给出) 时限$10 ...

随机推荐

  1. webapi 给自己挖的坑

    这次做项目,负责开发web api. 自己给自己挖了个坑.在所有的api接口前面都加上一个static . 结果检查路由配置.代码等等都找不到问题所在. 最后在一个同事的提醒下,原来是static给惹 ...

  2. python生成阿里云云直播推流播流地址

    申请一个阿里云账号,进入控制台,添加云直播工能,就可以获得相关数据, 直接上代码,阿里云接口文档https://cloud.tencent.com/document/product/267/7977 ...

  3. 淘宝内核月报2014-11-draft

    内核月报2014-11-draft MySQL· 捉虫动态·OPTIMIZE 不存在的表 bug 描述 这是一个和 GTID 相关的Bug,也就是说5.6才会有,并且出现这个 bug 需要满足条件: ...

  4. ADS 命令行命令介绍

    armasm 1. 命令:armasm [选项] -o 目标文件 源文件 2. 选项说明 -Errors 错误文件名        ;指定一个错误输出文件 -I 目录[,目录]          ;指 ...

  5. python代码{v: k for k, v in myArray.items()}是什么意思?

    最近在扒vnpy的源码总能看到{v: k for k, v in ORDERTYPE_VT2HUOBI.items()}这样的源码,就是不知道什么意思 然后万能的google找到了Quora的一个类似 ...

  6. P1487 失落的成绩单

    P1487 失落的成绩单a[i]=a[i-2]-2.0*a[i-1]+2.0*d;a[2]越大,a[3]越小a[3]越大,a[4]越小所以a[2]越大,a[4]越大,a[3]越小就有了单调性,分奇偶进 ...

  7. Ruby 安装 – Windows

    Ruby 安装 – Windows 下面列出了在 Windows 机器上安装 Ruby 的步骤. 注意:在安装时,您可能有不同的可用版本. - Window 系统下,我们可以使用 RubyInstal ...

  8. 牛客多校第八场 C CDMA 线性代数:沃尔什矩阵

    题意: 构造出一个由1和-1组成的$2^k*2^k$的矩阵,使得矩阵任意两列内积为0 题解: 数学知识,沃尔什矩阵.沃尔什矩阵的特性被CDMA(码分多址)采用,使得编码成为无线信号的频段和振幅之外的第 ...

  9. javascript html jquery 入门

    就开发难易程度来说,现在普遍使用jquery,本人学习jquery html css时间不长,以前写过Flex. CSS+JS+HTML组成HTML开发三驾马车.学习js开发我认为怎么入门十分重要.根 ...

  10. (转)Python学习笔记(1)__name__变量

    Python使用缩进对齐组织代码的执行,所有没有缩进的代码,都会在载入时自动执行.每个文件(模块)都可以任意写一些没有缩进的代码,并在载入时自动执行.为了区分 主执行代码和被调用文件,Python引入 ...