codeforces 498B. Name That Tune

题目写的比较难懂,题目大意:给你一个n,m代表有n首歌,在m秒内进行猜歌。让你求出m秒过后你能猜出来的歌曲的数量的期望。接下来n行,每行有两个数字第一个pi代表在歌曲结束前你猜出来的概率,第二个ti代表这首歌最多播放ti秒,在t秒之后你百分比之百能猜出来歌曲。最后输出m秒之后猜出来的歌曲的期望。

我们设dp[i][j]表示枚举到第i首歌,所用时间恰好为j的期望。

dp[i][j] 可以由状态dp[i-1][j-ti],dp[i-1][j-ti+1],......,dp[i-1][j-1]转移过来。

dp[i][j] = dp[i-1][j-ti]*(1-pi)^(ti-1)+dp[i-1][j-ti+1]*(1-pi)^(ti-2)*p+dp[i-1][j-ti+2]*(1-pi)^(ti-3)*p+......+dp[i-1][j-1]*p

需要注意的是dp[i][j-ti]此时第i首歌曲一定会猜中。所以dp[i][j] += dp[i-1][j-ti]*pow((1-pi), ti);

还有就是要减去之前无用的状态,dp[i-1][j-ti-1]的状态是推不到dp[i][j]的所以要减去。

设ans=dp[i-1][j-ti+1]*(1-pi)^(ti-2)+dp[i-1][j-ti+2]*(1-pi)^(ti-3)+......+dp[i-1][j-1],这样从j推向j+1时只要ans=ans*(1-pi)+dp[i-1][j]

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007

#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)

using namespace std;

inline int read()
{
    char ch;
    bool flag = false;
    int a = 0;
    while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
    if(ch != '-')
    {
        a *= 10;
        a += ch - '0';
    }
    else
    {
        flag = true;
    }
    while(((ch = getchar()) >= '0') && (ch <= '9'))
    {
        a *= 10;
        a += ch - '0';
    }
    if(flag)
    {
        a = -a;
    }
    return a;
}
void write(int a)
{
    if(a < 0)
    {
        putchar('-');
        a = -a;
    }
    if(a >= 10)
    {
        write(a / 10);
    }
    putchar(a % 10 + '0');
}

const int maxn = 5050;

struct node
{
    double p;
    int ti;
}f[maxn];

double dp[maxn][maxn];

int main()
{
    int n, m;
    while(cin >>n>>m)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf %d", &f[i].p, &f[i].ti);
            f[i].p /= 100.0;
        }
        for(int i = 0; i <= m; i++) dp[0][i] = 0;
        dp[0][0] = 1;
        double sum = 0.0;
        for(int i = 1; i <= n; i++)
        {
            double ans = 0;
            double q = pow(1-f[i].p, 1.0*f[i].ti);
            for(int j = 0; j <= m; j++)
            {
                if(j-1 >= 0) ans += dp[i-1][j-1];
                if((j-f[i].ti-1) >= 0) ans -= dp[i-1][j-f[i].ti-1]*q;
                dp[i][j] = ans*f[i].p;
                if(j-f[i].ti >= 0) dp[i][j] += dp[i-1][j-f[i].ti]*q;
                ans *= (1.0-f[i].p);
                sum += dp[i][j];
            }
        }
        printf("%.10lf\n", sum);
    }
    return 0;
}
时间: 2024-08-24 13:42:38

codeforces 498B. Name That Tune的相关文章

Codeforces 498B. Name That Tune 概率DP+优化

dp[i][j] 第i首歌在第j分钟听出来..... 一般情况下: dp[i][j]= dp[ i-1] [ j-k ] * p[i] * (1-p[i])^(k-1) 当k==t[i]时,一定可以听出来还要另加上 dp[ i-1] [ j-k ]*(1-p[i])^k 需要维护一段dp[i-1][k]的和,将时间复制度降到O(n^2) B. Name That Tune time limit per test 1 second memory limit per test 256 megabyt

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 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, Pet

498B Name That Tune

传送门 题目大意 n首音乐,第i首被听出来的概率为pi,刚开始听第一首,1s后如果听出来了则放第下一首,否则接着听这一首,第i首在连续听了ti s之后一定会被听出来,问Ts后听出来的歌的期望数量. 分析 我们非常容易想到dp[i][j]表示考虑前i首歌总共用了j秒的期望得分 但是我们发现转移复杂度O(T),总复杂度O(N^3) 于是我们考虑优化 我们想到对于dp[i][j]的一部分答案可以由dp[i][j-1]*(1-p[i])得到 于是转移复杂度优化到了O(1) 细节较多,详见代码 代码 #i

codeforces Name That Tune (概率dp)

题意: D - Name That Tune Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 499D Appoint description:  System Crawler  (2015-01-05) Description It turns out that you are a great fan of rock b

[Codeforces Round #284 (Div. 1) B]Name That Tune(概率Dp)

Description 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 so

Codeforces Gym 100187K K. Perpetuum Mobile 构造

K. Perpetuum Mobile Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description The world famous scientist Innokentiy almost finished the creation of perpetuum mobile. Its main part is the energy generator whic

Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一开始想把每个数的因子都找出来,找到这些因子中出现次数最多且因子大于n个数的最大公约数的,(n - 次数 )就是答案.但是复杂度是1e9,差那么一点. 自己还是对素数筛理解的不够深.这道题可以枚举素数x,对于每个x,找到所有(a[i]/gcd(all)) 是x倍数的个数,就是一个次数.找这个次数的过程

codeforces 1041 E. Tree Reconstruction 和度数有关的构造树

CF 1041E:http://codeforces.com/contest/1041/problem/E 题意: 告诉你一个树的节点个数,显然有n-1条边.已知去掉一条边后,两个集合中最大的节点值.问原来的树形状是怎么样的,构造不出来就输出NO. 思路: 这里说的“度数”可能有点不恰当.指以这个点引出一条链的长度,链上点的值小于这个点. 我想着这应该是可以作为一条链的,但是一直没有想到向节点度数上去想.首先,输入的一对值中,有一个一定是等于n的,那另一个值我们给它度数++.我们把度数为0的点从

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多