zoukankan      html  css  js  c++  java
  • nowcoder20 A 柯朵莉

    题意:一个序列长度为n,m次询问每次询问一个x,要求把这个序列分成每段不超过x,问分的段数的期望

    题解:好久没做期望了,都忘了。。。期望倒着算,dp[x]代表X后缀的期望,dp[n] = 1,只要双指针处理出来每个数能到的最有的下标,后缀保存一下,倒着算就可以了

    #include <bits/stdc++.h>
    #define maxn 101000
    #define INF 0x3f3f3f3f
    typedef long long ll;
    using namespace std;
    int a[maxn], x, n, m, l[maxn], sum;
    double dp[maxn], cnt[maxn];
    int main(){
        int ma = 0;
        scanf("%d%d", &n, &m);
        for(int i=1;i<=n;i++){
            scanf("%d", &a[i]);
            ma = max(a[i], ma);
        }
        while(m--){
            scanf("%d", &x);
            if(x<ma){
                printf("YNOI is good OI!
    ");
                continue;
            }
            sum = 0;
            int i=1,j=1;
            for(i=1,j=1;i<=n&&j<=n;i++){
                while(sum+a[j]<=x&&j<=n) sum += a[j], j++;
                l[i] = j-1;
                sum -= a[i];
            }
            for(int k=i;k<=n;k++) l[k] = n;
            cnt[n] = 1;
            for(int i=n-1;i>=1;i--){
                dp[i] = (cnt[i+1]-cnt[l[i]+2])*1.0/(l[i]-i+1)+1;
                cnt[i] = cnt[i+1]+dp[i];
            }
            printf("%.2f
    ", dp[1]);
        }
        return 0;
    }
  • 相关阅读:
    Search Insert Position
    *Set Matrix Zeroes
    Spiral Matrix II
    *Spiral Matrix
    combination的eclipse运行结果
    [?]*Combination(递归调用好难)
    [?]*Subset
    *3Sum Closest
    Why am I getting an Unreachable Statement error in Java?
    windows下,emacs的配置文件在哪儿?
  • 原文地址:https://www.cnblogs.com/Noevon/p/7818627.html
Copyright © 2011-2022 走看看