HDU 5481 Desiderium

Problem 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] .

1n100,000 .

1,000,000,000lr1,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
For the second sample, the excepted length is $\frac{0+2+2+3}{4}=\frac{7}{4}$.
 

Source

解题思路:周六实验室聚餐,所以这一次BC只能。。。。,今天下午来了坐了一下,发现自己也只能做出前两道题了,后面两道,感觉最后一题自己可以做做,但是正确与否我也不知道。。。。
求期望,我们可以采用这样一种方式打比方对于一个区间[a,b]如果被x个大的区间覆盖,则这个区间对最终区间长度的贡献为(b-a)*(2^x-1)*(2^(n-x)),这样我们只需要计算区间覆盖的次数即可,由于题目给出的l,r范围比较大,因此我们首先应该离散化,然后进行统计,最后计算结果即可。
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 110010;
const ll mod = 1000000007;
map<ll, ll> myMap;
ll l[maxn], r[maxn];
vector<ll> vec;
ll cnt[2*maxn];

ll power_mod(ll a, ll b) {
    ll ret = 1;
    while(b) {
        if(b&1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

int main() {

    //freopen("aa.in", "r", stdin);
    int T, n;
    ll ans;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        vec.clear();
        myMap.clear();
        ans = 0;
        for(int i = 1; i <= n; ++i) {
            scanf("%I64d %I64d", &l[i], &r[i]);
            vec.push_back(l[i]);
            vec.push_back(r[i]);
        }
        sort(vec.begin(), vec.end());
        vec.erase(unique(vec.begin(), vec.end()), vec.end());
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= n; ++i) {
            int s = lower_bound(vec.begin(), vec.end(), l[i]) - vec.begin();
            int e = lower_bound(vec.begin(), vec.end(), r[i]) - vec.begin();
            cnt[s]++;
            cnt[e]--;
        }
        int t_n = vec.size();
        for(int i = 1; i < t_n; ++i) {
            cnt[i] += cnt[i-1];
        }
        for(int i = 0; i < t_n; ++i) {
            myMap[vec[i]] = cnt[i];
        }
        map<ll, ll>::iterator it = myMap.begin();
        map<ll, ll>::iterator pit = it; it++;
        for( ; it != myMap.end(); ++it, ++pit) {
            ll x = pit->second;
            ans = (ans + (((it->first - pit->first) * (power_mod(2, x) - 1)) % mod * power_mod(2, n - x)) % mod) % mod;
        }
        if(ans < 0) ans += mod;
        printf("%I64d\n", ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值