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

用 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 )的更多相关文章

  1. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  2. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

  3. Codeforces 498B Name That Tune 概率dp (看题解)

    Name That Tune 刚开始我用前缀积优化dp, 精度炸炸的. 我们可以用f[ i ][ j ] 来推出f[ i ][ j + 1 ], 记得加加减减仔细一些... #include<b ...

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

  5. Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp

    一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\ ...

  6. Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)

    题目链接:New Year and Arbitrary Arrangement 题意: 有一个ab字符串,初始为空. 用Pa/(Pa+Pb)的概率在末尾添加字母a,有 Pb/(Pa+Pb)的概率在末尾 ...

  7. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...

  8. Codeforces 1139D(期望dp)

    题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...

  9. CodeForces 24D Broken robot(期望+高斯消元)

    CodeForces 24D Broken robot 大致题意:你有一个n行m列的矩形板,有一个机器人在开始在第i行第j列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...

随机推荐

  1. 攻防世界--insanity

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/d2a7dde552e647688230e80b2767b912 1.准备 获得信息: ...

  2. mod_jk是Apache服务器的一个可插入模块

    mod_jk简称JK,是Apache服务器的一个可插入模块,用以为Apache或IIS服务器提供处理JSP/Servlet的能力. Apache作为一款强大的Web服务器,本身缺乏处理JSP/Serv ...

  3. 2019-1-25-WPF-ListBox-的选择

    title author date CreateTime categories WPF ListBox 的选择 lindexi 2019-01-25 21:43:17 +0800 2018-2-13 ...

  4. 98-基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡)

    基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡) 1.板卡概述  板卡采用xilinx Spartan6系列芯片,支持 PCI Express Base Specificatio ...

  5. JS window对象 返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL。 语法: window.history.back();

    返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL. 语法: window.history.back(); 比如,返回前一个浏览的页面,代码如下: window.hi ...

  6. H5手机端底部菜单覆盖中间部分内容的解决办法

    一.第一种Js动态计算中间内容的高度. 二.第二种给底部上面写个<div style="底部的高度"></div> 三.第三种给中间部分写一个margin- ...

  7. Gradle构建SpringBoot并打包可运行的jar配置

    使用Gradle构建项目,继承了Ant的灵活和Maven的生命周期管理,不再使用XML作为配置文件格式,采用了DSL格式,使得脚本更加简洁. 构建环境: jdk1.6以上,此处使用1.8 Gradle ...

  8. SQL执行顺序和coalesce以及case when的用法

    1.mysql的执行顺序 from on join where group by having select distinct union   //UNION 操作符用于合并两个或多个 SELECT ...

  9. 【NLP新闻-2013.06.16】Representative Reviewing

    英语原文地址:http://nlp.hivefire.com/articles/share/40221/ 注:本人翻译NLP新闻只为学习专业英语和扩展视野,如果翻译的不好,请谅解! (实在是读不大懂, ...

  10. Echarts--Y坐标标题显示不全

    如:下图,Y坐标标题显示不全 y2可以控制不显示区域的高度,就能显示全啦 grid:{ x:40, x2:100, y2:200 }