10529-dumb bones (probability + interval DP)

Source: Internet
Author: User

Question connection: Ultraviolet A 10529-dumb bones

Given N, it indicates that N dominoes are to be put. Each time you put down the dominoes, the probability of descending to the left is PL, and the probability of descending to the right is PR. If you fall down, all the dominoes on that side will be pushed down. You can choose to place the dominoes one by one and ask what is the expected minimum number of dominoes.

Solution: DP [I] indicates the expected number of steps required to put one card, maintaining an optimal position, DP [I] = min \ {(steps from I-1 to I)} + (Steps 0 to i-1 )}

  • (Steps from I-1 to I): DP [I? J? 1]? PL + dp [J]? PR + 11? Pl? PR
  • (Steps from 0 to i-1): DP [J] + dp [I? J? 1]

So: DP [I] = min {DP [I? J? 1]? PL + dp [J]? PR + 11? Pl? PR + dp [J] + dp [I? J? 1]}

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 1005;const double INF = 0x3f3f3f3f3f3f3f;int N;double pl, pr, dp[maxn];double solve () {    double ac = 1 - pl - pr;    for (int i = 1; i <= N; i++) {        dp[i] = INF;        for (int j = 0; j < i; j++) {            double tmp = dp[i-j-1] * pl + dp[j] * pr;            tmp = dp[j] + dp[i-j-1] + (tmp + 1) / ac;            if (tmp < dp[i])                dp[i] = tmp;        }    }    return dp[N];}int main () {    while (scanf("%d", &N) == 1 && N) {        scanf("%lf%lf", &pl, &pr);        printf("%.2lf\n", solve());    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.