10529-dumb bones (probability + interval DP)

Source: Internet
Author: User
Ultraviolet A 10529-dumb bones

Question Link

Q: You try to arrange some dominoes in a straight line and then push them down. However, if you accidentally knocked down the bone card when you put it, it would knock down all the adjacent series of bone cards, and your work has been partially damaged. For example, you have placed the dominoes in the shape of DD _ dxddd_d, and want to place another dominoes at the position of X. It may knock down a bone card on the left or three bone cards on the right, and you will have to place them again. This kind of mistake cannot be avoided, but you can apply a special method to put the dominoes in one direction. The number of dominoes you want to place, and the probability of reverse to left and right when you place the dominoes, calculate the average number of dominoes you want to complete the task. Suppose you use the best placement policy. The input will contain up to 100 test points, each of which occupies one line, including the number of bone cards to be placed N (1 ≤ n ≤ 1000), and two non-negative real numbers PL, PR, indicates the probability of the left and right sides of the dominoes. Ensure 1 <PL + Pr ≤ 0.5. The last test point contains a value of 0. Retain two decimal places for the number of output questions for each test point.

Train of Thought: probability, + interval DP, DP [I] indicates that there is I continuous dominoes, so each time you want to make up I, you can choose any position in the middle, divide this card into two parts: DP [l] and DP [R], and then consider putting it on one. If you touch the left side, you need to move it on the left side. The right side is the same. According to the expected formula, the average step 1/(1-pl-Pr) can be used as a power amplifier, that is to say, there will be bumps before, the expected number of steps is (1 + dp [l] PL + dp [R] Pr ), so it is expected to be (1 + dp [l] PL + dp [R] Pr)/(1-pl-Pr) + dp [l] + dp [R], and the state transition equation is
DP [I] = min (calculation probability (DP [L], DP [R]) {calculate the center position of the enumeration L, r}

So the recursive solution of this question can pass, but the complexity is O (n ^ 2). In fact, there is something that can be optimized.

During dynamic planning, when the DP [I] array is looking for the minimum value, the equation actually satisfies a lower concave function, so this step can actually be solved by three points, the complexity is O (nlog (N). Then, in fact, for the lower concave function, it will not reduce the position of the minimum value next time, therefore, if the location of the last answer is recorded in each maintenance record, the time complexity can be optimized to O (n)

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define INF 0x3f3f3f3fconst int N = 1005;int n;double p, pl, pr, dp[N];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] = INF;for (int j = pre; j < i; j++) {    int l = j, r = i - 1 - j;    double tmp = dp[l] + dp[r] + (pl * dp[l] + pr * dp[r] + 1) / p;    if (dp[i] > tmp) {dp[i] = tmp;pre = j;    }}    }    return dp[n];}int main() {    while (~scanf("%d", &n) && 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.