hdu5481 Desiderium
链接
题意
给定n条线段,从中选取若干条,共有2n种选法(因为每一条线段有两种方法:选或者不选).
每一种选法都对应一个长度,也就是所选线段的并集长度.
求这2n种选法长度之和.
解法:
把这n条线段进行离散化,使得任意一条线段都可以由若干条元线段组成.
什么叫元线段呢?把全部的x坐标进行排序,去重,就得到了很多元线段.
统计每条元线段出现的次数,也就是说它被这n条线段里面的多少个线段覆盖.
如合统计呢?假设元线段被m条线段覆盖,那么有n-m条线段不覆盖它.
所以,该元线段被2n种选法中的2n-2n-m种选法覆盖.
ans=累加(元线段的长度*元线段使用的次数).
写法:
主要是如何统计元线段出现的次数.
- 建立线段树,把没一条线段进行插入O(nlgn)
- 树状数组O(nlgn)
- 普通数组O(n),这种方法最好,不仅简单,而且快.
代码(树状数组版):
#include<iostream> #include<string.h> #include<stdio.h> #include<math.h> #include<stdlib.h> #include<algorithm> using namespace std; #define re(i,n) for(int i=0;i<n;i++) typedef long long ll; const int maxn = 1e5 + 7; const int mod = 1e9 + 7; int n; struct Re{ int l, r; }a[maxn]; int x[maxn << 1], xi; int tr[maxn << 1]; #define lson(x) x<<1,f,mid #define rson(x) x<<1|1,mid+1,r int lowbit(int x){ return x&-x; } void ins(int x, int v){ for (int i = x; i > 0; i -= lowbit(i)){ tr[i] += v; } } int query(int x){ int ans = 0; for (int i = x; i < xi; i += lowbit(i)){ ans += tr[i]; } return ans; } int data[maxn]; int f(int m){ return data[n] - data[n - m]; } void init(){ data[0] = 1; re(i, maxn - 2){ data[i + 1] = ((ll)data[i] << 1) % mod; } } int cnt[maxn << 1]; int main(){ //freopen("in.txt", "r", stdin); init(); int T; cin >> T; while (T--){ scanf("%d", &n); xi = -1; re(i, n){ scanf("%d%d", &a[i].l, &a[i].r); x[++xi] = a[i].l, x[++xi] = a[i].r; } ++xi; sort(x, x + xi); xi = unique(x, x + xi) - x; memset(tr, 0, sizeof(tr)); re(i, n){ int beg = lower_bound(x, x + xi, a[i].l) - x; int over = lower_bound(x, x + xi, a[i].r) - x; ins(beg, -1), ins(over, 1); } cnt[0] = 0; for (int i = 1; i < xi; i++)cnt[i] = query(i); //re(i, xi)printf("(%d,%d) ", i, cnt[i]); //puts(""); ll ans = 0; re(i, xi - 1){ ll len = (ll)x[i + 1] - x[i]; int ge = cnt[i + 1]; //cout << len << " " << ge << endl; ans = (len*f(ge) + ans) % mod; } if (ans < 0)ans += mod; printf("%lld\n", ans); } return 0; }
代码(普通数组版):
#include<iostream> #include<algorithm> #include<stdio.h> using namespace std; typedef long long ll; #define re(i,n) for(int i=0;i<n;i++) ; ; struct Node{ int l, r; }a[maxn]; int n; ], xi; ]; int data[maxn]; void init(){ data[] = ; ; i < maxn-; i++){ data[i] = (data[i - ] << ) % mod; } } int main(){ //freopen("in.txt", "r", stdin); int T; cin >> T; init(); while (T--){ scanf("%d", &n); xi = -; re(i, n)scanf("%d%d", &a[i].l, &a[i].r), x[++xi] = a[i].l, x[++xi] = a[i].r; sort(x, x + xi + ); xi = unique(x, x + xi + ) - x; re(i, xi)cnt[i] = ; re(i, n){ int f = lower_bound(x, x + xi, a[i].l) - x, t = lower_bound(x, x + xi, a[i].r) - x; cnt[t]++, cnt[f]--; } ; i >= ; i--){ cnt[i] += cnt[i + ]; } ll ans = ; re(i, xi-){ ] - x[i]; ]; ans = (ans + (ll)len*(data[n] - data[n - m])) % mod; } )ans += mod; printf("%lld\n", ans); } ; }
hdu5481 Desiderium的更多相关文章
- HDU 5481 Desiderium 动态规划
Desiderium Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=548 ...
随机推荐
- 描述Linux shell中单引号,双引号及不加引号的简单区别(计时2分钟)
简要总结: 单引号: 可以说是所见即所得:即将单引号内的内容原样输出,或者描述为单引号里面看到的是什么就会输出什么. 双引号: 把双引号内的内容输出出来:如果内容中有命令.变量等,会先把变量.命令解析 ...
- java 自动装箱自动拆箱
1.Java数据类型 在介绍Java的自动装箱和拆箱之前,我们先来了解一下Java的基本数据类型. 在Java中,数据类型可以分为两大种,Primitive Type(基本类型)和Reference ...
- Mysql数据库笔记
出错记录: 1.mysql服务启动不了,进程意外终止 1067 错误显示:can not connect to mysql server on local hosts(1061) 解决方法 ...
- TFS 2013 生成(构建)历史记录保持策略(Retention Policy)
TFS服务器通过自动构建,实现软件生成和发布的自动化过程,这一直是TFS系统中非常重要的一个功能模块.近年来发布的TFS版本,都在自动化构建方面大幅增强了相应的功能.在这篇博客里我主要总结TFS 20 ...
- css多栏自适应布局
css多栏自适应布局还是需要总结一下的,都是基本功. 一般使用position属性布局,或者用float属性布局,也可以使用display属性. 看资料说position适合首页布局,因为首页内容往往 ...
- [转]how to split the ng-repeat data with three columns using bootstrap
本文转自:http://stackoverflow.com/questions/21644493/how-to-split-the-ng-repeat-data-with-three-columns- ...
- OpenStack Networking overview
原文地址:http://docs.openstack.org/newton/install-guide-ubuntu/neutron-concepts.html Networking service ...
- AC日记——验证字串 openjudge 1.7 18
18:验证子串 总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个字符串,验证其中一个串是否为另一个串的子串. 输入 输入两个字符串, 每个字符串占一行,长度不超过200且不 ...
- Java写的斗地主游戏源码
源码下载在最后 我们的前年的课设要求做一个斗地主程序,当时正在愁如何做界面,当时刚好在学习C#,于是就用C#完成了这个程序.一方面,当时我C#功底还很差(其实现在也不怎么样),很多地方用了“笨办法”, ...
- java 22 - 10 多线程之两种代码实现方式的比较与区别