CodeForces 499D. Name That Tune(概率dp)

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.

Examples

input

Copy

2 250 210 1

output

Copy

1.500000000

input

Copy

2 20 2100 2

output

Copy

1.000000000

input

Copy

3 350 350 225 2

output

Copy

1.687500000

input

Copy

2 20 20 2

output

Copy

1.000000000

题意:给出n首歌,每首歌长度为t[i]分钟,在歌曲播放的每分钟里都有p[i]的概率猜出歌名,如果猜出就会跳下一首,否则等这首歌放完了也会切下一首,求T秒时听歌数的期望。题解:令dp[i][j]表示第j分钟恰好听出第i首歌的概率,那么答案就是所有的dp[i][j](i<=n,j<=t)之和考虑转移:这首歌在第j秒听完的概率只会从他之前t[i]秒转移过来,所以dp的转移如下dp[i][j]=∑(dp[i-1][j-k]*(1-p[i])^(k-1)*p[i])+dp[i][j-t[i]]*(1-p[i])^(t[i]-1)(k<t[i]);

代码如下:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

double dp[5050][5050],p[5050];
int n,t,tt[5050];

double kasumi(double a,int b)
{
    double ans=1.0;
    while(b)
    {
        if(b&1)
        {
            ans=ans*a;
        }
        a=a*a;
        b>>=1;
    }
    return ans;
}

int main()
{
    scanf("%d%d",&n,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%d",&p[i],&tt[i]);
        p[i]/=100;
    }
    dp[0][0]=1;
    double ans=0.0;
    for(int i=1;i<=n;i++)
    {
        double sum=0;
        double kth=kasumi(1-p[i],tt[i]-1);
        for(int j=1;j<=t;j++)
        {
            sum*=1-p[i];
            sum+=dp[i-1][j-1]*p[i];
            if(j>=tt[i])
            {
                sum-=dp[i-1][j-tt[i]]*kth*p[i];
                dp[i][j]+=dp[i-1][j-tt[i]]*kth;
            }
            dp[i][j]+=sum;
            ans+=dp[i][j];
        }
    }
    printf("%.6lf\n",ans);
}
 

原文地址:https://www.cnblogs.com/stxy-ferryman/p/9571208.html

时间: 2024-11-12 13:55:49

CodeForces 499D. Name That Tune(概率dp)的相关文章

Codeforces 498B. Name That Tune 概率DP+优化

dp[i][j] 第i首歌在第j分钟听出来..... 一般情况下: dp[i][j]= dp[ i-1] [ j-k ] * p[i] * (1-p[i])^(k-1) 当k==t[i]时,一定可以听出来还要另加上 dp[ i-1] [ j-k ]*(1-p[i])^k 需要维护一段dp[i-1][k]的和,将时间复制度降到O(n^2) B. Name That Tune time limit per test 1 second memory limit per test 256 megabyt

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

codeforces Name That Tune (概率dp)

题意: D - Name That Tune Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 499D Appoint description:  System Crawler  (2015-01-05) Description It turns out that you are a great fan of rock b

Codeforces 148D Bag of mice (概率dp)

D. Bag of mice time limit per test:2 seconds memory limit per test:256 megabytes The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, wh

Codeforces 518D Ilya and Escalator (概率dp)

Ilya and Escalator time limit per test: 2 seconds memory limit per test: 256 megabytes Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Let's assume that

Codeforces 513G1 513G2 Inversions problem 概率dp

题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列, 每次操作等概率翻转一个区间,操作k次. 问: k次操作后逆序数对个数的期望. 思路: dp[i][j]表示 a[i] 在a[j] j前面的概率 初始就是 dp[i][j]  = 1( i < j ) 则对于翻转区间 [i, j], 出现的概率 P = 1 / ( n * (n+1) /2) 并且会导致 [i, j]内元素位置交换,枚举这次翻转的区间时所有的转移情况 #include <stdio.h> #inc

CodeForces 148D-Bag of mice(概率dp)

题意: 袋子里有w个白球b个黑球,现在两个人轮流每次取一个球(不放回),先取到白球的获胜,当后手取走一个球时,袋子里的球会随机的漏掉一个,问先手获胜的概率. 分析: dp[i][j]表示袋子中i个白球j个黑球,先手取获胜的概率. 有四种情况 先手取到白球,获胜概率1.0*i/(i+j); 后手取到白球,先手输 前两次都取到黑球,漏掉一个黑球,转移到dp[i][j-3] 前两次都取到黑球,漏掉一个白球,转移到dp[i-1][j-2] #include <map> #include <set

codeforces Problem-518D:Ilya and Escalator(概率dp)

传送门 题意:一共有n个人排着队,排在队首的人每一秒有p的概率上车,求过了t秒后车内的人数的期望值. 题解:用dp[i][j]表示第i秒有j个人的概率,状态转移方程为:dp[i][j]=p*dp[i-1][j-1]+(1-p)*dp[i-1][j](i<n),dp[i][j]=p*dp[i-1][j-1]+dp[i-1][j](i==n); AC代码: #include <bits/stdc++.h> using namespace std; const int maxn=2e3+4;

Codeforces 513C Second price auction 概率dp 求期望

题目链接:点击打开链接 题意: 有n个人去竞拍一件商品,下面给出n个区间表示每个人出的价是区间中随机的一个数(概率均等) 则第一名需要付的钱是第二名的竞拍价格(允许并列第一名) 求支付的钱的期望. 思路: 枚举付的钱,然后求付这个钱的概率,相乘后求和即可. 对于确定支付x元 分类讨论一下: 1.第一名出价大于x 枚举第一名,然后剩下来的人至少一个人出x元,其他人出<=x, P(剩下来的人一个人出x元,其他人出<=x) = P(剩下来的人出价<=x) - P(剩下的人出价<x) 2.