CodeForces 398B 概率DP 记忆化搜索
来源:互联网 发布:淘宝放心淘是什么 编辑:程序博客网 时间:2024/05/16 00:55
题目:http://codeforces.com/contest/398/problem/B
有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了很久也没有想出来,有点无从下手的感觉,最后还是尝试了一下记忆化搜索,以dp[0][0]为边界,dp[x][y]代表当前有x行y列没有彩色的 瓷砖,搜索起来思路还是很清晰的,可惜最后那个 算期望公式给写错了,瞎了好久,做出以后看了一下别人的做法,确实有点难想到,把涂好的都放在右下角,这样就只有四种情况了,瓷砖移动肯定是有影响的,后来理解了他们那意思是 划分西线把瓷砖划分到右下角去,这样 状态转移 其实跟记忆化搜索的一模一样了,想不到那个转移,
int n,m;int xx[20000 + 55],yy[20000 + 55];double dp[2000 + 55][2000 + 55];int cntx = 0,cnty = 0;void init() {memset(xx,0,sizeof(xx));memset(yy,0,sizeof(yy));memset(dp,-1,sizeof(dp));}bool input() {while(cin>>n>>m) {cntx = cnty = n;for(int i=0;i<m;i++) {int x,y;scanf("%d %d",&x,&y);if(!xx[x])cntx--;if(!yy[y])cnty--;xx[x]++;yy[y]++;}return false;}return true;}double dfs(int nowx,int nowy) {if(dp[nowx][nowy] > -1.0 + eps)return dp[nowx][nowy];if(nowx == 0 && nowy == 0)return dp[nowx][nowy] = 0.00;double p = nowx * 1.0/n;double q = nowy * 1.0/n;double ans = 1.00;if(nowx != 0)ans += dfs(nowx - 1,nowy) * p * (1.0 - q);if(nowy != 0) ans += dfs(nowx,nowy - 1) * (1.0 - p) * q;if(nowx != 0 && nowy != 0)ans += dfs(nowx - 1,nowy - 1) * p * q;return dp[nowx][nowy] = ans/(1.00 - (1.0 - p) * (1.0 - q));}void cal() {double ans = dfs(cntx,cnty);printf("%.10lf\n",ans);}void output() {}int main() {while(true) {init();if(input())return 0;cal();output();}return 0;}
0 0
- CodeForces 398B 概率DP 记忆化搜索
- codeforces 148D 概率dp 记忆化搜索
- HDU 5001 概率DP || 记忆化搜索
- uva1637 概率dp(记忆化搜索)
- Codeforces Div.301D Bad Luck Island(概率dp+记忆化搜索)
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
- codeforces B. Cow Program (记忆化搜索)
- Codeforces 208 B Solitaire(记忆化搜索)
- codeforces (数位dp 记忆化搜索)
- codeforces 721C 记忆化搜索 / dp
- 【SPOJ-TOMJERRY】Tom and Jerry【概率DP】【记忆化搜索】
- dp(记忆化搜索) + 概率论(全概率)
- hdu3559 Frost Chain (概率dp+记忆化搜索)
- 概率DP,记忆化搜索(纸牌游戏,uva 1637)
- poj_3071 Football(概率dp+记忆化搜索)
- codeforces 424E Colored Jenga (状态压缩,概率dp用hash记忆优化搜索)
- codeforces 392B Tower of Hanoi 记忆化搜索
- codeforces 520B Two Buttons[记忆化搜索]
- 正则表达式详解
- Instrumenting Android Apps with Soot
- Tomcat性能调优方案
- 为什么C语言里不推荐强制转换malloc的返回值?
- C#常见编译错误
- CodeForces 398B 概率DP 记忆化搜索
- android 工具类篇 ResourceUtils
- USACO Friday the Thirteenth
- C#程序将DLL包进EXE方法
- java多线程实战( 多个线程 修改同一个变量)
- Java中的接口
- 【人生成就达成】【#1】
- Kaldi安装实录(二)
- Android ContentProvider Cursor