zoukankan      html  css  js  c++  java
  • HDU 5481 Desiderium 动态规划

    Desiderium

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=5481

    Description

    There is a set of intervals, the size of this set is n.

    If we select a subset of this set with equal probability, how many the expected length of intervals' union of this subset is?

    We assume that the length of empty set's union is 0, and we want the answer multiply 2n modulo 109+7.

    Input

    The first line of the input is a integer T, meaning that there are T test cases.

    Every test cases begin with a integer n ,which is size of set.

    Then n lines follow, each contain two integers l,r describing a interval of [l,r].

    1≤n≤100,000.

    −1,000,000,000≤l≤r≤1,000,000,000.

    Output

    For every test case output the answer multiply 2n modulo 109+7. 

    Sample Input

    2
    1
    0 1
    2
    0 2
    1 3

    Sample Output

    1
    7

    HINT

    题意

    有一条数轴,还有一个区间的集合,集合大小为n。
    现在等概率的从集合中选出集合的一个子集,求取出的子集的区间并集的期望长度。
    空集的区间并长度被认为是0。

    题解:

    实际上计算的是所有子集的并集长度之和。

    把坐标离散化之后,可以单独考虑每一小段区间在并集内部的出现次数,如果有mm个大区间覆盖这段小区间,就会发现当前仅当这mm个区间都不在子集中时,这一小段区间不会成为并集中的一部分,所以一共有2^n-2^m2
    ​​个子集包含这段小区间。把长度乘出现次数累计到答案里即可

    代码:

    //qscqesze
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 300006
    #define mod 1000000007
    #define eps 1e-9
    #define e exp(1.0)
    #define PI acos(-1)
    const double EP  = 1E-10 ;
    int Num;
    //const int inf=0x7fffffff;
    const ll inf=999999999;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //*************************************************************************************
    
    int p[maxn];
    struct node{
        int x,y;
    };
    node dp2[maxn];
    bool cmp(node a,node b)
    {
        if(a.x!=b.x) return a.x<b.x;
        return a.y<b.y;
    }
    int dp[maxn];
    void pre()
    {
        p[0]=1;
        for(int i=1;i<maxn;i++)
        {
            long long tmp=2LL*p[i-1];
            if(tmp>=mod) tmp-=mod;
            p[i]=(int)tmp;
        }
    }
    int main()
    {
        pre();
        int t=read();
        while(t--)
        {
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                long long tmp=(long long)(p[i]-1);
                if(tmp<0) tmp+=mod;
                tmp*=(long long)(p[n-i]);
                if(tmp>=mod) tmp%=mod;
                dp[i]=(int)tmp;
            }
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&dp2[i<<1].x,&dp2[i<<1|1].x);
                dp2[i<<1].y=1;dp2[i<<1|1].y=-1;
            }
            sort(dp2,dp2+2*n,cmp);
            int ans1=0;
            ll ans2=0;
            for(int i=0;i<2*n-1;i++)
            {
                int l=dp2[i].x,r=dp2[i+1].x;
                ans1+=dp2[i].y;
                ans2+=(ll)(r-l)*(ll)dp[ans1];
                if(ans2>=mod) ans2%=mod;
            }
            printf("%I64d
    ",ans2);
        }
        return 0;
    }
  • 相关阅读:
    Python常用转换函数
    Python随机数
    sublime text的pylinter插件设置pylint_rc后提示错误
    使用Pydoc生成文档
    字符编码笔记:ASCII,Unicode和UTF-8
    Windows编程MessageBox函数
    魔方阵算法及C语言实现
    iOS通讯录整合,兼容iOS789写法,附demo
    谈谈iOS app的线上性能监测
    ReactiveCocoa代码实践之-更多思考
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4841401.html
Copyright © 2011-2022 走看看