Codeforces 492B Name That Tune ( 期望DP )
1 second
256 megabytes
standard input
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.
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 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.
2 2
50 2
10 1
1.500000000
2 2
0 2
100 2
1.000000000
3 3
50 3
50 2
25 2
1.687500000
2 2
0 2
0 2
1.000000000
用 dp[i][j] 表示唱到第 i 首歌 , 用了j个时间点, 还能唱多少首歌的期望。
dp的状态不难想出来
记忆化搜索的时间复杂度是O( n ^ 3 ) 的 , 大数据超时。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
#define X first
#define Y second
const int N = ;
const double eps = 1e-;
double dp[N][N] , P[N] , T[N] ;
bool vis[N][N];
int n , t ; double DP( int i , int j ){
if( j > t || i >= n ) return ;
if( !vis[i][j] ) {
double p = 1.0 , tmp = 1.0 ; dp[i][j] = ;
for( int k = ; k < T[i] && k + j <= t ; ++k ){
if( tmp >= eps ) {
tmp = p * P[i] * ( DP( i + , j + k ) + 1.0 );
dp[i][j] += tmp ;
}
p *= ( 1.0 - P[i] );
}
if( j + T[i] <= t ) dp[i][j] += p * ( DP( i+,j+T[i] ) + 1.0 );
vis[i][j] = true ;
}
return dp[i][j];
} int main(){
// freopen("in.txt","r",stdin);
while( cin >> n >> t ) {
memset( vis , false , sizeof vis );
for( int i = ; i < n ; ++i ) {
cin >> P[i] >> T[i] ;
P[i] /= ;
}
printf("%.10lf\n",DP(,));
}
}
因为歌是要一首首按顺序唱的 。
因为求得是期望, 所以要从后向前处理。
本来更新的时候是先要枚举歌曲,然后枚举用了的时间,再枚举唱的时间。
对于第i首跟第i+1首歌( 注意逆向 )的转移:
dp[i][j] += ( 1 - p[i] )^( t - 1 ) * p[i] * dp[i+1][ j - t ] ;
其实我们可以发现,更新的时候可以利用提取公因子来化简公式之后,可以把第二维降了 。
就变成O(n^2)。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
#define X first
#define Y second
const int N = ;
const double eps = 1e-;
double dp[N][N] , P[N] ;
int n , t , T[N] ;
int main(){
// freopen("in.txt","r",stdin);
while( cin >> n >> t ) {
// memset( dp , 0 , sizeof dp );
for( int i = ; i < n ; ++i ) {
cin >> P[i] >> T[i] ;
P[i] /= ;
}
for( int i = n - ; i >= ; --i ) {
double p = 1.0 - P[i] , pp = 1.0 ;
dp[i][] = ;
for( int j = ; j < T[i] ; ++j ) pp *= 1.0-P[i];
for( int j = ; j <= t ; ++j ) {
dp[i][j] = p * dp[i][j-] + P[i]*( dp[i+][j-] + 1.0 ) ;
if( j == T[i] ){
dp[i][j] += pp ;
}
else if( j > T[i] ) {
dp[i][j] += pp * ( dp[i+][j-T[i]] - dp[i+][j-T[i]-] ) ;
}
}
}
printf("%.10lf\n",dp[][t]);
}
}
Codeforces 492B Name That Tune ( 期望DP )的更多相关文章
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Codeforces 498B Name That Tune 概率dp (看题解)
Name That Tune 刚开始我用前缀积优化dp, 精度炸炸的. 我们可以用f[ i ][ j ] 来推出f[ i ][ j + 1 ], 记得加加减减仔细一些... #include<b ...
- 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 followi ...
- Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp
一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\ ...
- Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)
题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...
- 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP
[题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...
- Codeforces 1139D(期望dp)
题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...
- CodeForces 24D Broken robot(期望+高斯消元)
CodeForces 24D Broken robot 大致题意:你有一个n行m列的矩形板,有一个机器人在开始在第i行第j列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...
随机推荐
- struts2的action方法匹配以及通配符的使用
1. ActionMethod:Action执行的时候并不一定要执行execute方法,可以在配置文件中配置action的时候用"method"属性来指定执行哪个方法,也可以在ur ...
- 【问题解决方案】关于Python中的语句 ' %matplotlib inline '
跟进小项目#GirlsInAI#-可视化时遇到的语句,之前没有遇到过 在Stack Overflow上看到了一个解释: IPython有一组预定义的"魔术函数",您可以使用命令行样 ...
- elasticsearch 基础 —— Explain、Version、min_score、query rescorer
Explain 相关度得分计算: GET /_search { "explain": true, "query" : { "term" : ...
- shell简单的菜单功能
- smbpasswd - Samba加密的口令文件。
总览 SYNOPSIS smbpasswd 描述 DESCRIPTION 此文件是 Samba(7) 套件的一部分. smbpasswd是Samba加密的口令文件.文件中包含了用户名,UNIX用户ID ...
- smbmnt - 装载 SMB 文件系统的协助工具
总览 smbmnt mount-point [ -s share ] [ -r ] [ -u uid ] [ -g gid ] [ -f mask ] [ -d mask ] 描述 smbmnt 用于 ...
- javaScript的预加载
在有大量图片的页面中,为了避免页面加载完图片还未加载完成,我们通常会使用js的图片预加载. 这是一个预加载的demo: 首先把图片放入到一个类名为imgSrcArr的变量当中: var imgSrcA ...
- python常用函数 H
heapify(iterable) 堆排序. 例子: heappop(iterable) 弹出堆排序的第一个元素,即最小值. 例子: hasattr(object,attr) 用于确定对象是否有某个属 ...
- 再读js正则表达式
正则表达式定义 在js中有两种方式来定义正则表达式, 第一种是类似perl的语法来定义一个正则表达式,我们把它叫做正则表达式字面量法: var expression = /pattern/flag 其 ...
- Java的GC机制及算法
GC的阶段 对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finalize的方法. reclamation: ...