BestCoder Round #20 B
来源:互联网 发布:出国英文翻译软件 编辑:程序博客网 时间:2024/05/18 01:10
题目大意:输入T表示测试用例数,接下来输入N,表示有N组数据,每组有x,y表示X轴上的一段线段,最后找出被这N条线段覆盖最多的点的覆盖次数。
算法思想:
每条线段[x,y],可以理解为【x,y+1)的左闭右开的区间,从x点开始覆盖次数加1,当点到y+1时之前的覆盖结束覆盖次数-1。基于此思想,可以将所有的x,y+1排序,然后找出这2N个数的最大字段和,又因为x<=y,所以又可以理解为求最大前缀和问题。
代码如下:
#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int MAXN=200050;typedef struct { int xy;//记录x或y坐标 int value;}Locate;Locate line[MAXN];bool cmp(Locate a,Locate b){ if(a.xy<b.xy)return true; return false;}int main(){ int T,N,x,y,k; cin>>T; while(T--){ cin>>N; k=0; for(int i=0;i<N;i++){ cin>>x>>y; line[k].xy=x; line[k].value=1; k++; line[k].xy=y+1; line[k].value=-1; k++; } sort(line,line+k,cmp); int ans=0; int num=0; for(int i=0;i<k;i++){ ans+=line[i].value; num=max(ans,num); } cout<<num<<endl; } return 0;}
0 0
- BestCoder Round #20 B
- BestCoder Round #20 B,C
- BestCoder Round #80B
- BestCoder Round #3 A,B
- BestCoder Round #25 A,B
- BestCoder Round #77 (div2) B
- BestCoder Round #92 A+B
- BestCoder Round #20 解题报告 A.B.C.
- ACM-BestCoder Round #20
- BestCoder Round #20 A
- BestCoder Round #20
- BestCoder Round #20 1002
- BestCoder Round #20小结
- BestCoder Round #7-A,B,C
- BestCoder Round #8 B题 Reading comprehension
- BestCoder Round #8 A,B,C
- Fibonacci (BestCoder Round #28 B)
- BestCoder Round #29 A,B,C
- monogoDB
- 截止到11.30号最新版本的 adt-bundle & android-sdk_r23.0.2 下载地址
- 安卓自学笔记:17:两个Activity之间的切换
- 手把手教你使用PS切图
- 数据结构与算法5: 递归(Recursion)
- BestCoder Round #20 B
- 标准正态分布alpha分位点
- 图形结构的存储
- 启动 hadoop的产生问题
- 使安卓本地sdk中的文档能够打开
- 初步认识Shiro
- Google Dremel数据模型详解(上)
- 基于标准库实现string和wstring的转换
- LeetCode[Tree]: Populating Next Right Pointers in Each Node