Question link: Click the open link
Question:
Dominoes should be placed on a straight line.
Input n, l, R
To place n rows, the probability of moving down to the left is l, and the probability of moving to the right is R.
You can adopt the optimal strategy, that is, you can place a segment in the middle, and then place a segment on both sides of the left and right. The placement order is arbitrary.
Q: The expected number of times to place n cards under the best strategy.
Ideas:
Click Open Link
#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <map>#include <cmath>template <class T>inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1;}template <class T>inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if(x>9) pt(x/10); putchar(x%10+'0');}using namespace std;typedef long long ll;#define N 2002const ll mod = 1e9+7;int n;double l, r;double dp[N];double solve(){dp[0] = 0;dp[1] = 1.0/(1.0-l-r);for(int i = 2; i <= n; i++){dp[i] = 1e18;for(int j = 0; j < i; j++){int L = j, R = i-j-1;double x = (1+ dp[L] + dp[R] -dp[L]*r -dp[R]*l) / (1-l-r);dp[i] = min(dp[i], x);}}return dp[n];}int main() { while(cin>>n>>l>>r, n){ printf("%.2f\n", solve()); } return 0;}
Expectation for calculation of the probability DP of the WordPress 10529 dumb bones