ZOj3605--- Find the Marble(概率dp)
来源:互联网 发布:应用架构 数据架构 编辑:程序博客网 时间:2024/06/04 19:29
Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.
Unfortunately, Alice’s actions are very fast, so Bob can only catch k of m swappings and regard these k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal possibility.
Input
There are several test cases in the input file. The first line of the input file contains an integer N (N ≈ 100), then N cases follow.
The first line of each test case contains 4 integers n, m, k and s(0 < s ≤ n ≤ 50, 0 ≤ k ≤ m ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed from 1 to n. Then m lines follow, each of which contains two integers ai and bi (1 ≤ ai, bi ≤ n), telling the two pots Alice swaps in the i-th swapping.
Outout
For each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.
Sample Input
3
3 1 1 1
1 2
3 1 0 1
1 2
3 3 2 2
2 3
3 2
1 2
Sample Output
2
1
3
Author: GUAN, Yao
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
dp[i][j][k]表示Alice交换了i次,Bob看到了j次,marble在k位置的概率,转移分看到和没看到
可以预处理下50以内的组合数
总之还是很简单的一道dp
ps:换了下代码风格(其实只是{ 换了位置)
/************************************************************************* > File Name: ZOJ3605.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年04月15日 星期三 18时55分02秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;LL C[60][60];double dp[60][60][60];PLL exc[60];void Combine() { C[0][0] = 1; for (int i = 1; i <= 50; ++i) { C[i][0] = 1; C[i][1] = i; for (int j = 2; j < i; ++j) { C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; } C[i][i] = 1; }}int main() { int t; Combine(); scanf("%d", &t); while (t--) { int n, m, K, s, a, b; scanf("%d%d%d%d", &n, &m, &K, &s); memset(dp, 0, sizeof(dp)); dp[0][0][s] = 1; for (int i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); exc[i] = make_pair(a, b); } for (int i = 1; i <= m; ++i) { for (int j = 0; j <= i; ++j) { for (int k = 1; k <= n; ++k) { if (i - 1 >= j) { dp[i][j][k] += dp[i - 1][j][k] * (C[i - 1][j] * 1.0 / C[i][j]); } if (k != exc[i].first && k != exc[i].second) { dp[i][j][k] += dp[i - 1][j - 1][k] * (1 - C[i - 1][j] * 1.0 / C[i][j]); } else if (k == exc[i].first) { dp[i][j][k] += dp[i - 1][j - 1][exc[i].second] * (1 - C[i - 1][j] * 1.0 / C[i][j]); } else { dp[i][j][k] += dp[i - 1][j - 1][exc[i].first] * (1 - C[i - 1][j] * 1.0 / C[i][j]); } } } } double max_p = 0; int ans; for (int i = 1; i <= n; ++i) { if (max_p < dp[m][K][i]) { max_p = dp[m][K][i]; ans = i; } } printf("%d\n", ans); } return 0;}
- ZOJ3605-Find the Marble(概率DP)
- zoj3605 Find the Marble --- 概率dp
- ZOj3605--- Find the Marble(概率dp)
- zoj3605 Find the Marble(三维dp)
- ZOJ3605-Find the Marble
- zoj 3605 Find the Marble (概率dp)
- ZOJ 3605 Find the Marble(三维dp)
- ZOJ 3605 Find the Marble(DP)
- zoj 3605 Find the Marble 计数dp
- 【DP】ZOJ 3605 Find the Marble
- ZOJ 3605 Find the Marble(dp)
- ZOJ 3605-F - Find the Marble-DP
- zoj 3605 Find the Marble (dp)
- Find the Marble ZOJ
- ZOJ 3605 Find the Marble(dp啊 三维)
- ZOJ 3605Find the Marble
- ZOJ 3605 Find the Marble
- ZOJ 3605Find the Marble
- windows tomcat catalina.out 日志输出
- 阿里2015实习生笔试附加题第一个
- js-倒计时
- C# 存储单位转化
- 2014求职记(一)
- ZOj3605--- Find the Marble(概率dp)
- notifyDataSetChanged() 动态更新ListView
- libzdb源码学习之二:异常模块
- 第四章 基于对象的编程风格(什么是构造函数和析构函数)
- 右边的人
- hdu 1045 Fire Net
- Java 用socket实现网络通信
- 199. Binary Tree Right Side View Leetcode Python
- 第六周 课后实践:项目三——人数不定的工资类