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列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...
随机推荐
- iviewUI框架,使用table表格内render下拉框select被遮盖问题
使用props:{ transfer:true },即可 1.原本代码:
- 三、SQL Server 对JSON的支持
一.SQL Server 对JSON的支持 一.实现效果 现在 我用数据库是sql2008 ,共计2万数据. 每一条数据里面的有一个为attribute字段是 json存储状态属性, 我怎么查看 ...
- ajax提交表单包括文件
<script src="${pageContext.request.contextPath}/assets/js/jquery-1.8.3.js"></scri ...
- oracle多表连接方式Hash Join Nested Loop Join Merge Join
在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL) ...
- spring Boot 简单的登录功能,利用了jdbcTemplate.class完成sql语句的执行,无需service层、dao层和.xml文件
1.搭建SpringBoot项目首先我们先在IDEA上创建一个SpringBoot的Web项目(1)file ——> new ——> project——> Spring Initia ...
- [CSP-S模拟测试]:Star Way To Heaven(最小生成树Prim)
题目描述 小$w$伤心的走上了$Star\ way\ to\ heaven$. 到天堂的道路是一个笛卡尔坐标系上一个$n\times m$的长方形通道(顶点在$(0,0)$和$(n,m)$),小$w$ ...
- Cent OS (一)Cents OS的基本安装
1.实验环境: VMware Workstation Pro 14 Pro Cent OS 7 系列. 2. 镜像地址传送门: 阿里云开源镜像站:http://mirrors.aliyun.com ...
- 110、TensorFlow张量值的计算
import tensorflow as tf #placeholders在没有提供具体值的时候不能使用eval方法来计算它的值 # 另外的建模方法可能会使得模型变得复杂 # TensorFlow 不 ...
- Oracle 11g 体系结构 --SGA PGA 前后台进程
Oracle服务器主要由实例.数据库.程序全局区.前台进程 实例:用来提供管理数据库的功能 数据库:由Oracle数据库文件组成,用来存储系统数据 ;一般有:数据文件.控制文件.重做日志文件 而实例可 ...
- 测开之路四十二:常用的jquery事件
$(‘selector’).click() 触发点击事件$(‘selector’).click(function) 添加点击事件$(‘selector’).dbclick() 触发双击事件$(‘sel ...