码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-20 16:24:32      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:


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


/* ***********************************************
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;
}




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

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/44493171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!