498B Name That Tune (概率DP)

B. Name That Tune
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It turns out that you are a great fan of rock band AC/PE. Peter learned that and started the following game: he plays the first song of the list of n songs of the group, and you have to find out the name of the song. After you tell the song name, Peter immediately plays the following song in order, and so on.

The i-th song of AC/PE has its recognizability pi. This means that if the song has not yet been recognized by you, you listen to it for exactly one more second and with probability of pi percent you recognize it and tell it's name. Otherwise you continue listening it. Note that you can only try to guess it only when it is integer number of seconds after the moment the song starts playing.

In all AC/PE songs the first words of chorus are the same as the title, so when you've heard the first ti seconds of i-th song and its chorus starts, you immediately guess its name for sure.

For example, in the song Highway To Red the chorus sounds pretty late, but the song has high recognizability. In the song Back In Blue, on the other hand, the words from the title sound close to the beginning of the song, but it's hard to name it before hearing those words. You can name both of these songs during a few more first seconds.

Determine the expected number songs of you will recognize if the game lasts for exactly T seconds (i. e. you can make the last guess on the second T, after that the game stops).

If all songs are recognized faster than in T seconds, the game stops after the last song is recognized.

Input

The first line of the input contains numbers n and T (1 ≤ n ≤ 5000, 1 ≤ T ≤ 5000), separated by a space. Next n lines contain pairs of numbers pi and ti (0 ≤ pi ≤ 100, 1 ≤ ti ≤ T). The songs are given in the same order as in Petya's list.

Output

Output a single number — the expected number of the number of songs you will recognize in T seconds. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Sample test(s)
input
2 2
50 2
10 1
output
1.500000000
input
2 2
0 2
100 2
output
1.000000000
input
3 3
50 3
50 2
25 2
output
1.687500000
input
2 2
0 2
0 2
output
1.000000000
 
题意:这题题意真心难懂,就是给你n首歌,T代表总游戏时间,p代表你能猜出歌的概率,t代表歌曲的持续时间,每秒末你可以猜歌曲,一首猜出来后才能运行下首,在一首歌运行t秒后必定会被猜出,还有一个注意点:当你的游戏刚好持续T时间时候,你可以在游戏结束额外再猜一次,问你猜出n首歌的概率期望。
 
思路:解释下首个样例把:dp[i][j]表示枚举到i首歌,猜出来的时间为j。
第一首歌在1秒末猜出,dp[1][1]=dp[0][0]*0.5=0.5
第一首歌在2秒末猜出,dp[1][2]=dp[0][0]*(1-0.5)*0.5+dp[0][0]*(1-0.5)^2=0.25+0.25=0.5
第二首歌在1秒末猜出,dp[2][1]=dp[1][0]*0.1=0      显然不可能
第二首歌在2秒末猜出,dp[2][2]=dp[1][1]*0.1+dp[1][1]*(1-0.1)*1=0.5
所以总和为1.5
dp[i][j] = dp[i-1][j-ti]*(1-pi)^(ti-1) <这项就是ti时间都错最后额外猜一次> + dp[i-1][j-ti+1]*(1-pi)^(ti-2)*p + dp[i-1][j-ti+2]*(1-pi)^(ti-3)*p +......+ dp[i-1][j-1]*p,除了dp[i-1][j-ti]这项的系数特别一点外(因为第ti次一定能听出来),其他的所乘的系数就是一个等比数列。于是我们考虑记录前缀和,令ans=dp[i-1][j-ti+1]*(1-pi)^(ti-2)+dp[i-1][j-ti+2]*(1-pi)^(ti-3)+......+dp[i-1][j-1],这样从j推向j+1时只要ans=ans*(1-pi)+dp[i-1][j]即可。   注意!如果j-ti-1>=0我们需要减掉不能转移过来的状态dp[i-1][j-ti-1]*(1-pi)^ti

 

代码:

 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
const int maxn = 5050;

struct node
{
    double p;
    int ti;
} f[maxn];

double dp[maxn][maxn];
int main()
{
    int n, m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf %d", &f[i].p, &f[i].ti);
            f[i].p /= 100.0;
        }
        for(int i = 0; i <= m; i++) dp[0][i] = 0;
        dp[0][0] = 1;
        double sum = 0.0;
        for(int i = 1; i <= n; i++)
        {
            double ans = 0;
            double q = pow(1-f[i].p, 1.0*f[i].ti);
            for(int j = i; j <= m; j++)
            {
                ans += dp[i-1][j-1];
                if((j-f[i].ti-1) >= 0)//减去前面推来假如有的ti时间到不了的一项
                    ans -= dp[i-1][j-f[i].ti-1]*q;
                dp[i][j] = ans*f[i].p;
                if(j-f[i].ti >= 0)
                    dp[i][j] += dp[i-1][j-f[i].ti]*q;//这项就是ti时间都错最后额外猜一次
                ans *= (1.0-f[i].p);//等比数列多乘的(1-pi)
                sum += dp[i][j];
            }
        }
        printf("%.10lf\n", sum);
    }
    return 0;
}
View Code

 

当然可以用滚动数组优化:

 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
const int maxn = 5050;

struct node
{
    double p;
    int ti;
} f[maxn];

double dp[2][maxn];
int main()
{
    int n, m,cur=0;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf %d", &f[i].p, &f[i].ti);
            f[i].p /= 100.0;
        }
        for(int i = 0; i <= m; i++) dp[0][i] = 0;
        dp[0][0] = 1;
        double sum = 0;
        for(int i = 1; i <= n; i++)
        {
            double ans = 0;
            double q = pow(1-f[i].p, 1.0*f[i].ti);
            cur=cur^1;
            for(int j=0; j<i; j++)//这里的清零重要
                dp[cur][j] = 0 ;
            for(int j = i; j <= m; j++)
            {
                ans += dp[cur ^1][j-1];
                if((j-f[i].ti-1) >= 0)//减去前面推来假如有的ti时间到不了的一项
                    ans -= dp[cur ^1][j-f[i].ti-1]*q;
                dp[cur][j] = ans*f[i].p;
                if(j-f[i].ti >= 0)
                    dp[cur][j] += dp[cur ^1][j-f[i].ti]*q;//这项就是ti时间都错最后额外猜一次
                ans *= (1.0-f[i].p);//等比数列多乘的(1-pi)
                sum += dp[cur][j];
            }
        }
        printf("%.9lf\n", sum);
    }
    return 0;
}

View Code

 

posted @ 2015-02-07 14:15  Doli  阅读(294)  评论(0)    收藏  举报