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- ;
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';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[]=;
for(int i=;i<maxn;i++)
{
long long tmp=2LL*p[i-];
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=;i<=n;i++)
{
long long tmp=(long long)(p[i]-);
if(tmp<) tmp+=mod;
tmp*=(long long)(p[n-i]);
if(tmp>=mod) tmp%=mod;
dp[i]=(int)tmp;
}
for(int i=;i<n;i++)
{
scanf("%d%d",&dp2[i<<].x,&dp2[i<<|].x);
dp2[i<<].y=;dp2[i<<|].y=-;
}
sort(dp2,dp2+*n,cmp);
int ans1=;
ll ans2=;
for(int i=;i<*n-;i++)
{
int l=dp2[i].x,r=dp2[i+].x;
ans1+=dp2[i].y;
ans2+=(ll)(r-l)*(ll)dp[ans1];
if(ans2>=mod) ans2%=mod;
}
printf("%I64d\n",ans2);
}
return ;
}

HDU 5481 Desiderium 动态规划的更多相关文章

  1. [HDU 3535] AreYouBusy (动态规划 混合背包 值得做很多遍)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n个任务集合,需要在T个时间单位内完成.每个任务集合有属性,属性为0的代表至少要完成1个 ...

  2. [HDU 1114] Piggy-Bank (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 简单完全背包,不多说. #include <cstdio> #include < ...

  3. [HDU 2955]Robberies (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意是给你一个概率P,和N个银行 现在要去偷钱,在每个银行可以偷到m块钱,但是有p的概率被抓 问 ...

  4. HDU 2571 命运 动态规划

    命运 http://acm.hdu.edu.cn/showproblem.php?pid=2571 Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了!可谁能想到, ...

  5. 百度之星资格赛 hdu 4826 Labyrinth 动态规划

    /********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上 ...

  6. hdu 2059 龟兔赛跑(动态规划DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    M ...

  7. HDU 1069 基础动态规划+排序

    题意 给出n种立方体石头 当且仅当一块石头的底部宽度长度都小于一块石头的时候才能放在上面 问最高能放多高?石头不限数目 然而同样一种石头采用同样的摆放方式 两快相同石头一定无法进行放置 所以 一块石头 ...

  8. hdu 4571 floyd+动态规划

    思路: 我们先求一遍floyd,将各点的最短距离求出,然后将点按si的升序排序.dp[i][k]表示第i个点在第j时间所获得的最大效益,那么 dp[i][k]=max(dp[ i ][ k ]  , ...

  9. hdu 2583 permutation 动态规划

    Problem Description Permutation plays a very important role in Combinatorics. For example ,1 2 3 4  ...

随机推荐

  1. Android开发之全局获取Context的技巧

    转自<第一行代码-Android>进阶篇 这本书对于入门来说确实很棒,很简单明了的介绍了Android开发中涉及到的方方面面,对我的帮助很大,同时记录一些该书中一些对我以后开发有用的东西, ...

  2. Android开发之parseSdkContent failed Could not initialize class android.graphics.Typeface

    在进行android开发过程中,忽然发现经常弹出来parseSdkContent failed 这个错误,然后google了下解决办法 方法1: 删除.android文件 重启eclipse. 该方法 ...

  3. cf 189B - Counting Rhombi

    题目:189B - Counting Rhombi http://codeforces.com/problemset/problem/189/B 题意:给定一个长方形的 矩形,求能在这个矩形里有多少 ...

  4. Vs 引用第三方DLL文件 版本不一致问题 (npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案)

    最近在 做 MailChimp 与网站功能 集成时,发现 MailChimp 2API 中的 MailChimp.dll  中的依赖项 SerivceStack.Text.dll (版本为3.9.71 ...

  5. a标签中的label在IE下触发不了a标签的href链接(label标签——解析)

    <a href="http://www.baidu.com/" target="_blank"> <span>百度</span&g ...

  6. 事件处理程序(addEventListener 和 attachEvent)

    1.addEventListener 和 removeEventListener 接收三个参数:要处理的事件名.作为事件处理程序的函数和布尔值 缺点:不兼容IE <body> <in ...

  7. jQuery.Validate验证库

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  8. 本地连接图标消失;修改地址IP地址

    (1)网络连接中没有本地连接,电脑无法进行拨号.无法上网,右键点击“网上连接”选择“属性”,弹出的“网络连接”文件夹中没有本地连接的图标,类似情况处理起来要相对复杂些了,我们逐一判断故障原因,在想办法 ...

  9. sql-定义变量

    declare @subject nvarchar(50) set @subject=(select Subject from dbo.Scores where ID=1) --select @sub ...

  10. Storm入门

    Storm框架入门 1 Topology构成 和同样是计算框架的Mapreduce相比,Mapreduce集群上运行的是Job,而Storm集群上运行的是Topology.但是Job在运行结束之后会自 ...