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列,它每一步会随机从可以选择的方案里任选一个(向下走一格,向左走一格,向右走一 ...
随机推荐
- XMPP即时通讯协议使用(三)——订阅发布、断开重连与Ping
package com.testV3; import java.util.List; import org.jivesoftware.smack.ConnectionListener; import ...
- activiti 5.22 表结构解析及清空流程运行测试数据
1.结构设计 1.1. 逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: 'RE'表示repository(存储),RepositoryService接口所操作的 ...
- 125-FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块
FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块 1.板卡概述 该板卡可实现2路14bit 250Msps AD 和2路16bit 160MspsDA功能,FMC连接 ...
- liunx-网络基础
liunx 网络配置 ifconfig: ipconfig -a ;显示信息 ifconfig eth1 up //开启网络接口 ifconfig eth1 down //关闭网络接口 ifconf ...
- django允许外部访问 项目后台不挂断运行
1关闭防火墙 1 service iptables stop 2设置django 1 2 3 4 5 6 7 8 9 10 11 开开启django时,使用0.0.0.0:xxxx,作为ip和端口例如 ...
- ini配置文件内如果有更新参数
ini配置文件内如果有更新参数 执行更新 更新参数 自动去下载执行????
- 【leetcode】336. Palindrome Pairs
题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...
- android android studio error
SIMPLE: Error computing //cmake 包含的跨平台头文件或者是源文件路径出错
- 4,JPA
一,什么是JPA JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA(Java Pers ...
- Windows系统启动iis方法详解
很多网友一般都用Windows 系统自带的iis服务器来配置web网站,在本地进行调试和修改后才正式上线.虽说操作不难,但是小白来说却无从下手,很多人根本不知道iss在哪,怎么启动,更谈不上配置或者其 ...