Dumb Bones UVA-10529 (probabilistic DP)

Source: Internet
Author: User

Test instructions

You try to line up some dominoes and push them down. But if you accidentally knocked the dominoes out of the dominoes when you put them on, it would knock down a bunch of dominoes,

And your work has been partially destroyed.

For example, you have put the dominoes in the shape of a dd__dxddd_d and want to place a domino in the X position. It may knock the left piece of dominoes or the three dominoes on the right, and you will have to rearrange the dominoes.

This error cannot be avoided, but you can apply a special method of putting dominoes to make the dominoes fall more in one direction.

Give the number of dominoes you want to place, and the probability that it will fall to the left and right when you put the dominoes, to calculate the average number of dominoes that you have placed for the completion of the task. Let's say you use the best placement strategy.

The input will contain up to 100 test points, one row per test point, and a number of dominoes to be placed n? (1≤n≤1000), and two non-negative real pl,? Pr, indicating the probability of the dominoes falling to the left and right. Guaranteed 1

Analytical:

Let's say we're on the left and right side of the I I, so there's three things left to do.

The desired number of times to the left of the set is the ER on the right side of El

Then ei equals the probability of multiplying the probabilities and then adding them.

If not inverted Ei = El + Er + 1;

If it's left, then we're going to have to go back to the left and increase El + 1 times? Think carefully, of course not. Because if we put it down again, it's ei-er.

Similarly, if the right side is Ei-el

So Ei = El + Er + 1 + (ei-er) * PL + (ei-el) * PR;

Move Items to

ei = min (ei, (1-P1)/(1-P1-P2) * El + (1-P2)/(1-P1-P2) * Er + 1/(1-P1-P2);

#include <iostream>
#include<cstdio>#include<sstream>#include<cstring>#include<map>#include<cctype>#include<Set>#include<vector>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<bitset>#defineRap (i, A, n) for (int i=a; i<=n; i++)#defineRep (I, A, n) for (int i=a; i<n; i++)#defineLap (I, a, n) for (int i=n; i>=a; i--)#defineLEP (i, A, n) for (int i=n; i>a; i--)#defineRD (a) scanf ("%d", &a)#defineRlld (a) scanf ("%lld", &a)#defineRC (a) scanf ("%c", &a)#defineRS (a) scanf ("%s", a)#definePD (a) printf ("%d\n", a);#definePlld (a) printf ("%lld\n", a);#definePC (a) printf ("%c\n", a);#definePS (a) printf ("%s\n", a);#defineMOD 2018#defineLL Long Long#defineULL unsigned long Long#definePair Pair<int, int>#defineMem (A, B) memset (A, B, sizeof (a))#define_ Ios_base::sync_with_stdio (0), Cin.tie (0)//freopen ("1.txt", "R", stdin);using namespacestd;Const intMAXN =1700, INF =0x7fffffff, Ll_inf =0x7fffffffffffffff;intN, m, tot;DoubleDP[MAXN];intMain () {intN; DoubleP1, p2; while(Cin >> n >> P1 >>p2) { for(intI=1; i<=n; i++) {Dp[i]=INF; for(intj=0; j<i; J + +) Dp[i]= Min (Dp[i], (1-P1)/(Double) (1-P1-P2) * Dp[j] + (1-P2)/(Double) (1-P1-P2) * Dp[i-j-1]); Dp[i]+=1/ (Double)(1-P1-p2); } printf ("%.2f\n", Dp[n]); } return 0;}

Dumb Bones UVA-10529 (probabilistic DP)

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.