BZOJ4008: [HNOI2015] King Arthur

Description

Little K was accidentally brainwashed by the LL cult, to the extent that he even wanted to get out of the King Arthur cult.

He decided to play King Arthur one last time before getting out of the pit. Since it's the last battle, it must be done well
Bright. As we all know, King Arthur is a face-to-face game, and the activation of skills is based on probability. as a non-
Continental, and as a former OIer, Xiao K naturally wants to maximize the expected value of damage. but he has
I haven't written code for many years, and I can't even type Spaly wrong. Therefore, I hope you can help Xiao K and make him feel a little better.
What is it like to be a European. 
In this problem we will consider a simplified model of the game. 
The player has a set of cards, a total of n cards. During the game, the player arranges n cards in a certain order.
Number the cards from 1 to n from front to back. In this question, the order has been determined, that is, the order of input.
Each card has a skill. The skill activation probability of the i-th card is pi. If it is successfully activated, it will
The enemy deals di damage. Only by activating skills, cards can cause damage to the enemy. based on real
Considering the prime and the African ancestry of the little K, pi will not be 0, nor will it be 1, that is, 0 < pi < 1. 
A game has a total of r rounds. In each round, the system will start with the first card, in order
Consider each card. In a round, for each card considered in turn: 
1 If this card has activated skills in this game, then 
1.1 If this card is not the last one, skip it (consider the next card); 
Otherwise (the last one), end the round. 
2 Otherwise (this card has not activated skills in this game), set this card as the ith card 
2.1 Activate the skill with the probability of pi. 
2.2 If the skill is activated, cause di damage to the enemy and end the round. 
2.3 If this card is already the last one (i.e. i is equal to n), end the round; otherwise,
Consider the next card. 
Please help Little K to find the expected value of damage that this set of cards can cause in one game. 

Input

The first line of the input file contains an integer T, representing the number of test data sets. 

Next, there are a total of T groups of data. 
The first line of each set of data contains two integers n and r separated by spaces, representing the number of cards and
The number of rounds of the game. 
The next n lines, each containing a real number and an integer, separated by spaces, describe a card. the first
The two numbers in row i are pi and di, which represent the probability (real number) and skill activation of the i-th card skill activation respectively.
Damage dealt (integer). Guarantee that pi contains at most 4 decimal places and is a legal probability. 

Output

 For each set of data, output a line containing a real number, which is the result of this set of cards in this game.

expected damage. For each line of output, only if the relative error between your output and the standard answer is no more than
When 10^-8 - i.e. |ao|/a<=10-8 (where a is the standard answer and o is the output), your output will be judged correct.
It is recommended to output 10 decimal places. 

Sample Input

1
3 2
0.5000 2
0.3000 3
0.9000 1

Sample Output

3.2660250000

HINT

 

 There are 13 possible situations: 


1. In the first round, the first card activates the skill; in the second round, the second card activates the skill; 

The probability is 0.15 and the damage is 5. 

2. In the first round, the first card activates the skill; in the second round, the third card activates the skill; 

The probability is 0.315 and the damage is 3. 

3. In the first round, the first card activates the skills; the second round does not activate the skills; 

The probability is 0.035 and the damage is 2. 

4. In the first round, the second card activates the skill; in the second round, the first card activates the skill; 

The probability is 0.075 and the damage is 5. 

5. In the first round, the second card activates the skill; in the second round, the third card activates the skill; 

The probability is 0.0675 and the damage is 4. 

6. In the first round, the second card activates skills; in the second round, no skills are activated; 

The probability is 0.0075 and the damage is 3. 

7. In the first round, the third card activates the skill; in the second round, the first card activates the skill; 

The probability is 0.1575 and the damage is 3. 

8. In the first round, the third card activates the skill; in the second round, the second card activates the skill; 

The probability is 0.04725 and the damage is 4. 

9. In the first round, the third card activates skills; in the second round, no skills are activated; 

The probability is 0.11025 and the damage is 1. 

10. No skills are activated in the first round; in the second round, the first card activates skills; 

The probability is 0.0175 and the damage is 2. 

11. The first round does not activate the skills; in the second round, the second card activates the skills; 

The probability is 0.00525 and the damage is 3. 

12. No skills are activated in the first round; in the second round, the third card activates skills; 

The probability is 0.011025 and the damage is 1. 

13. No skills will be activated in the first round; no skills will be activated in the second round; 

The probability is 0.001225 and the damage is 0. 

The expected value of causing damage is the sum of the probability multiplied by the corresponding damage, which is 3.266025. 
 
For all test data, 1 <= T <= 444, 1 <= n <= 220, 0 <= r <= 132, 0 < pi < 1, 0 <= di <= 1000.  
 
Unless otherwise specified in the remarks, pi and di in the data are randomly generated. 

Be aware of possible real precision issues and take appropriate action. 
 
 
 
This question is not very easy to think, but it is easy to figure out? ? I feel that every time I do the probability expectation dp is like this bird, almost
After reading the title of the meeting, you can find that the order in which each card is thrown out is useless. If you throw out 1324, it is actually no different from 1234.
Then it can be concluded that this is a brand that expects dp (are you kidding me??)
It is better to think about transfer, let f[i][j] represent the current processing to the i-th card, and there are j rounds
Then there are two situations:
1. Not in front
2. The i-1th card is played, and the game is over
Then the dog's brain can get the transfer formula (look at my code), and then accumulate the answers.
In fact, there are several ideas for dp.
 
code show as below:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int T,n,r;
double ans,p[310],f[310][310],d[310];
double pow_mod(double a,int b)
{
    double ans=1.0;
    while(b)
    {
        if(b%2==1)ans=ans*a;
        b/=2;a*=a;
    }
    return ans;
}
intmain ()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&r);
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;i++)scanf("%lf%lf",&p[i],&d[i]);
        f[0][r]=1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=r;j++)
                f[i][j]=f[i-1][j]*pow_mod(1-p[i-1],j)+f[i-1][j+1]*(1-pow_mod(1-p[i-1],j+1));
        ans=0.0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=r;j++)
                ans+=f[i][j]*(1-pow_mod(1-p[i],j))*d[i];
        printf("%.10lf\n",ans);
    }
    return 0;
}

by_lmy

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325163410&siteId=291194637