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 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
/* ***********************************************
Author        :CKboss
Created Time  :2015年03月20日 星期五 14时25分28秒
File Name     :CF498B_2.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=5500;

int n,T;
double dp[maxn][maxn];
int t[maxn];
double p[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	scanf("%d%d",&n,&T);
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%d",p+i,t+i); p[i]/=100.;
	}

	dp[0][0]=1;
	double ans=0;

	for(int i=1;i<=n;i++)
	{
		double P=p[i],VP=1-p[i],VPN=pow(VP,t[i]);
		double temp=0;

		for(int j=0;j<=T;j++)
		{
			if(j-1>=0) temp+=dp[i-1][j-1];
			if(j-t[i]-1>=0) temp-=dp[i-1][j-t[i]-1]*VPN;
			dp[i][j]+=temp*P;
			if(j-t[i]>=0) dp[i][j]+=dp[i-1][j-t[i]]*VPN;
			temp=temp*VP;
			ans+=dp[i][j];
		}
	}
	printf("%.11lf\n",ans);

    return 0;
}
时间: 2024-08-28 21:59:39

Codeforces 498B. Name That Tune 概率DP+优化的相关文章

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, Pet

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

UVALive-8138 Number Generator 概率dp+优化

题目链接:https://cn.vjudge.net/problem/UVALive-8138 题意 有一个随机数生成器,输出1-n的整数. 现在已经输出了k个数,问再取几个数才能使取出的所有数的个数至少为2. 注意T<=1e5, \sum k<=1e5 思路 (听说存在公式?理论上说有了转移方程和边界,公式就是存在 概率dp,注意状态的选取. 设i为出现0次的数的个数,j为出现1次的数的个数. \[ \begin{align*} dp(i, j) &= \frac{i}{n}[dp(

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 498B. Name That Tune

题目写的比较难懂,题目大意:给你一个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]转移过来

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