codeforces 498B. Name That Tune

畫船聽雨發表於2015-01-19

題目寫的比較難懂,題目大意:給你一個n,m代表有n首歌,在m秒內進行猜歌。讓你求出m秒過後你能猜出來的歌曲的數量的期望。接下來n行,每行有兩個數字第一個pi代表在歌曲結束前你猜出來的概率,第二個ti代表這首歌最多播放ti秒,在t秒之後你百分比之百能猜出來歌曲。最後輸出m秒之後猜出來的歌曲的期望。

我們設dp[i][j]表示列舉到第i首歌,所用時間恰好為j的期望。

dp[i][j] 可以由狀態dp[i-1][j-ti],dp[i-1][j-ti+1],......,dp[i-1][j-1]轉移過來。

dp[i][j] = dp[i-1][j-ti]*(1-pi)^(ti-1)+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][j-ti]此時第i首歌曲一定會猜中。所以dp[i][j] += dp[i-1][j-ti]*pow((1-pi), ti);

還有就是要減去之前無用的狀態,dp[i-1][j-ti-1]的狀態是推不到dp[i][j]的所以要減去。

設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]

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 ≤ 50001 ≤ T ≤ 5000), separated by a space. Next n lines contain pairs of numbers pi and ti (0 ≤ pi ≤ 1001 ≤ 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

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007

#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)

using namespace std;


inline int read()
{
    char ch;
    bool flag = false;
    int a = 0;
    while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
    if(ch != '-')
    {
        a *= 10;
        a += ch - '0';
    }
    else
    {
        flag = true;
    }
    while(((ch = getchar()) >= '0') && (ch <= '9'))
    {
        a *= 10;
        a += ch - '0';
    }
    if(flag)
    {
        a = -a;
    }
    return a;
}
void write(int a)
{
    if(a < 0)
    {
        putchar('-');
        a = -a;
    }
    if(a >= 10)
    {
        write(a / 10);
    }
    putchar(a % 10 + '0');
}

const int maxn = 5050;

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

double dp[maxn][maxn];

int main()
{
    int n, m;
    while(cin >>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 = 0; j <= m; j++)
            {
                if(j-1 >= 0) ans += dp[i-1][j-1];
                if((j-f[i].ti-1) >= 0) 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;
                ans *= (1.0-f[i].p);
                sum += dp[i][j];
            }
        }
        printf("%.10lf\n", sum);
    }
    return 0;
}


相關文章