Cool routine of yizaki III, cool
Source: https://biancheng.love/contest-ng/index.html#/34/problemsquestion description
When there is a lot of free time, I like to play some very time-consuming games, such as I wanna, which can be killed thousands of times. If I'm not careful, I need to start over again.
When a domino card is placed, an avalanche will occur if you hit it halfway. For example11__ 1x11_11In this shape, the person who is so dead will certainly put a bone card in the center x ...... This method of death includes:PlThe probability will be reversed to the left and the one on the left will be knocked down, orPrThe probability will be reversed to the right and the two on the right will be knocked down. (The bone card is not a quantum state. It won't be either left or right ......)
Now, I want to place the N dominoes in my hand in a straight line. What is the expected number of dominoes?
Input
Multiple groups of input data.
Each data group contains three numbers, the first is an integer N <10000, And the next two floating point numbers pl, pr, 0 <pl + pr <1
Output
For each group of data, output a row. The expected number of times when the best placement method is adopted will be retained with two decimal places.
Input example
2 0.1 0.110 0.2 0.3
Output example
2.6644.03
Hint
Expected geometric distribution Ex = 1/p
O (n ^ 2) can pass
Code:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define INF 0x3f3f3f3fconst int N = 1000005;int n;double p, pl, pr, dp[N];double cal(int l, int r) { return dp[l] + dp[r] + (pl * dp[l] + pr * dp[r] + 1) / p;}double solve() { p = 1 - pl - pr; dp[0] = 0; dp[1] = 1 / p; int pre = 0; for (int i = 2; i <= n; i++) { dp[i] = cal(pre, i - pre - 1); for (int j = pre + 1; j < i; j++) { int l = j, r = i - 1 - j; double tmp = cal(l, r); if (dp[i] >= tmp) { dp[i] = tmp; pre = j; } else break; } } return dp[n];}int main() { while (~scanf("%d", &n) && n) { scanf("%lf%lf", &pl, &pr); printf("%.2lf\n", solve()); } return 0;}