hdu 6305(多校第一场)

题意:定义RMQ(A,l,r)为序列A满足A[i] = max(A[l],A[l+1],......,A[r])中最小的i。如果RMQ(A,l,r)= RMQ(B,l,r),则称他们相似。给出A序列,B序列中元素为0-1的实数,求出满足RMQ(A,l,r)= RMQ(B,l,r)的B序列所有元素和的期望。

思路:找出所有与A同构的笛卡尔树。对于每一个节点,与A同构的概率为这个节点子树的大小(包括这个节点)。那么总的概率为\prod_{i=1}^{n} \frac{1}{sz[i]},因为B在0-1上均匀分布,所以B得期望为\frac{1}{2},则和的期望为\frac{n}{2},所以要求的总期望为\frac{n}{2}\prod_{i=1}^{n}\frac{1}{sz[i]}.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>

using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int maxn = 1e6+10;
const int inf = 0x3f3f3f3f;
ll inv_[maxn];
struct node
{
    int val,sz;
    int l,r,pre;
}st[maxn];
stack<int>s;

void init(int n)
{
    for(int i = 0; i <= n; i++)
    {
        st[i].l = st[i].r = st[i].pre = st[i].sz = 0;
    }
    st[0].val = inf;
    while(!s.empty())
        s.pop();
    s.push(0);
}

void build(int n)
{
    for(int i = 1; i <= n; i++)
    {
        while(!s.empty()&&st[s.top()].val<st[i].val) s.pop();
        int pre = s.top();
        st[i].pre = pre;
        st[i].l = st[pre].r;
        st[st[pre].r].pre = i;
        st[pre].r = i;
        s.push(i);
    }
}

void dfs(int u)
{
    if(u==0)
        return;
    st[u].sz = 1;
    dfs(st[u].l);
    dfs(st[u].r);
    st[u].sz += st[st[u].l].sz + st[st[u].r].sz;
}

void inv()
{
    inv_[1] = 1;
    for(int i = 2; i < maxn; i++)
    {
        inv_[i] = inv_[mod%i]*(mod-mod/i)%mod;
    }
}

int main()
{
    int T,n;
    inv();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init(n);
        for(int i = 1; i <= n; i++)
            scanf("%d",&st[i].val);
        build(n);
        dfs(st[0].r);
        ll ans = 1;
        for(int i = 1; i <= n; i++)
        {
            ans = ans*inv_[st[i].sz]%mod;
        }
        ans = (ans*n)%mod;
        ans = (ans*inv_[2])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值