zoukankan      html  css  js  c++  java
  • Codeforces 492B Name That Tune ( 期望DP )

    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 = 5011;
    const double eps = 1e-20;
    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 0;
        if( !vis[i][j] ) {
            double p = 1.0 , tmp = 1.0 ; dp[i][j] = 0 ;
            for( int k = 1 ; k < T[i] && k + j <= t ; ++k ){
                if( tmp >= eps ) {
                    tmp = p * P[i] * ( DP( i + 1 , j + k  ) + 1.0 );
                    dp[i][j] += tmp ;
                }
                p *= ( 1.0 - P[i] );
            }
            if( j + T[i] <= t ) dp[i][j] += p * ( DP( i+1,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 = 0 ; i < n ; ++i ) {
                cin >> P[i] >> T[i] ;
                P[i] /= 100 ;
            }
            printf("%.10lf
    ",DP(0,0));
        }
    }
    View Code

    因为歌是要一首首按顺序唱的 。

    因为求得是期望, 所以要从后向前处理。

    本来更新的时候是先要枚举歌曲,然后枚举用了的时间,再枚举唱的时间。

    对于第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 = 5011;
    const double eps = 1e-20;
    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 = 0 ; i < n ; ++i ) {
                cin >> P[i] >> T[i] ;
                P[i] /= 100 ;
            }
            for( int i = n - 1 ; i >= 0 ; --i ) {
                double p = 1.0 - P[i] , pp = 1.0 ;
                dp[i][0] = 0 ;
                for( int j = 0 ; j < T[i] ; ++j ) pp *= 1.0-P[i];
                for( int j = 1 ; j <= t ; ++j ) {
                    dp[i][j] = p * dp[i][j-1] + P[i]*( dp[i+1][j-1] + 1.0 ) ;
                    if( j == T[i] ){
                        dp[i][j] += pp ;
                    }
                    else if( j > T[i] ) {
                        dp[i][j] += pp * ( dp[i+1][j-T[i]] - dp[i+1][j-T[i]-1] ) ;
                    }
                }
            }
            printf("%.10lf
    ",dp[0][t]);
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    条款04:确定对象在使用前已经被初始化
    条款06:若不想使用编译器自动生成的函数,就应该明确拒绝
    计算机操作系统之死锁的原因和必要条件
    条款10:令operator=返回一个reference to *this
    条款02:尽量以const,enum,inline代替#define
    条款11:在operator=处理自我赋值
    计算机操作系统之进程与线程
    堆排序
    NodeJS For Windows
    我常用的linux命令
  • 原文地址:https://www.cnblogs.com/hlmark/p/4185376.html
Copyright © 2011-2022 走看看