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列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...
随机推荐
- JavaScript——实现继承的几种方式
实现继承的6中方法: 借用构造函数 组合继承 原型式继承 寄生式继承 寄生组合式继承 拷贝继承 1. 借用构造函数 在子类型构造函数的内部调用超类构造函数.通过使用apply()和call()方法在新 ...
- XMPP即时通讯协议使用(四)——Openfire服务器源码编译与添加消息记录保存
下载Openfire源码 下载地址:https://www.igniterealtime.org/downloads/index.jsp,当前最新版本为:4.2.3 Eclipse上部署Openfir ...
- 关于如何测试cpu性能的命令操作 linux系统
for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/d ...
- linux安装jdk环境(多种方式)
通过tar.gz压缩包安装 此方法适用于绝大部分的linux系统 1.先下载tar.gz的压缩包,这里使用官网下载. 进入: http://www.oracle.com/technetwork/jav ...
- Polish orthography
Computer encoding[edit] There are several different systems for encoding the Polish alphabet for com ...
- HTML基础:用表单写一个简易登录页面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- windows下搭建Mongo主(Master)/从(slave)数据库同步
需要启动两个mongoDb文档数据库,一个是主模式启动,另一个是属于从模式启动. 1. 创建主从服务器 主服务器:192.168.1.131:27017 备服务器:192.168.1.131:2701 ...
- BZOJ3398 牡牛和牝牛
隔板搞掉就行了 特么我什么时候感冒能好!QAQ.脑子都没有了QAQ. //Love and Freedom. #include<cstdio> #include<cstring> ...
- 存储过程如何传变量到like下
存储过程中执行如下DDL语句create or replace procedure etl_test(v_com varchar2) is v_spname varchar2(40); com var ...
- 数据挖掘:周期性分析SMCA算法
数据挖掘:周期性分析SMCA算法 原文地址:http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1423978 算法介绍 以时间顺序挖掘周期性的模式 ...