HDU 5124 lines
2015-11-12 14:39
197 查看
lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1439 Accepted Submission(s): 594
Problem Description
John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A.
Input
The first line contains a single integer T(1≤T≤100)(the
data for N>100 less
than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105),indicating
the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109),describing
a line.
Output
For each case, output an integer means how many lines cover A.
Sample Input
2 5 1 2 2 2 2 4 3 4 5 1000 5 1 1 2 2 3 3 4 4 5 5
Sample Output
3 1
Source
BestCoder Round #20
有两种方法:
第一种是直接把端点排序,起点的值为1,终点的值为-1,然后过一遍取最大值。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> #include<vector> #include<map> #include<cmath> #include<fstream> #include<stack> using namespace std; typedef long long ll; struct node { int x, v; }s[200020]; int cmp(node a, node b) { if(a.x == b.x) return a.v>b.v; return a.x <b.x; } int main() { int t, n; cin>>t; while(t--) { scanf("%d", &n); for(int i = 0; i<n; i++) { scanf("%d", &s[i<<1].x); s[i<<1].v = 1; scanf("%d", &s[i<<1|1].x); s[i<<1|1].v = -1; } sort(s, s+2*n, cmp); int cnt = 0, ans = 0; for(int i = 0; i<2*n; i++) { cnt += s[i].v; ans = max(cnt , ans); } cout<<ans<<endl; } }第二种:先离散化,然后线段树区间更新,取最大值。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> #include<vector> #include<map> #include<cmath> #include<fstream> using namespace std; struct node { int l,r, maxx, mark; } s[200200<<2]; int x[200020], k, a[100010], b[100010]; void pushup(int cnt) { s[cnt].maxx = max(s[cnt<<1].maxx , s[cnt<<1|1].maxx); } void pushdown(int cnt) { if(s[cnt].mark) { //int l = len - (len/2); s[cnt<<1].mark += s[cnt].mark ; s[cnt<<1|1].mark += s[cnt].mark; s[cnt<<1].maxx += s[cnt].mark; s[cnt<<1|1].maxx += s[cnt].mark; s[cnt].mark = 0; } } void build_tree(int l, int r, int cnt) { s[cnt].l = l; s[cnt].r = r; s[cnt].maxx = 0; s[cnt].mark = 0; if(l == r) return ; int m= (l+r)>>1; build_tree(l, m, cnt<<1); build_tree(m+1, r, cnt<<1|1); } void update(int l, int r, int cnt) { if(s[cnt].l >= l && s[cnt].r <= r) { s[cnt].mark += 1; s[cnt].maxx += 1; return ; } pushdown(cnt); int m = (s[cnt].l + s[cnt].r)>>1; if(l <= m) update(l, r, cnt<<1); if(r > m) update(l, r, cnt<<1|1); pushup(cnt); } int main() { int t, n, u, v; cin>>t; while(t--) { scanf("%d", &n); k = 0; for(int i = 1; i<=n; i++) { scanf("%d%d", &a[i], &b[i]); x[k++] = a[i]; x[k++] = b[i]; } sort(x,x+2*n); //for(int i = 0; i<2*n; i++) // printf("%d ", x[i]); // printf("\n"); build_tree(1, 2*n, 1); for(int i = 1; i<=n; i++) { int l = lower_bound(x, x+2*n, a[i]) - x + 1; int r = lower_bound(x, x+2*n, b[i]) - x + 1; // printf("%d %d %d %d\n", l, r, a[i], b[i]); update(l, r, 1); } printf("%d\n", s[1].maxx); } return 0; }
相关文章推荐
- Edittext自定软键盘Enter键功能
- ArcGIS Server JS 开发在线新增要素同时记录添加人、添加时间出现的问题
- Laravel 5中使用 illuminate/html扩展
- 龟兔赛跑算法(Floyd判圈算法)
- JavaScript标准Selection操作
- JS中文转换(UTF-8),中文乱码解决办法,url传递中文乱码解决
- knockoutjs中使用mapping插件绑定数据列表
- hdu 4696 Answers(规律)
- RAC添加节点遇到的错误
- Bluemix 动手系列 (二) 通过命令行部署和更新云应用
- linux如何安装jdk
- svn status 返回值详解
- chrome,IE上传图片,js获取图片绝对路径的方法
- 为自己打造Linux小系统
- json字符串和json对象相互转化
- 原型链
- 谈谈Tempdb对SQL Server性能优化有何影响
- OkHttp使用教程
- 解决pybrain的No module named 'structure' 问题
- 问题记录:在打包APK的时候遇到的问题