lines(最大区间和)
lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1350 Accepted Submission(s): 558
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.
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.
5
1 2
2 2
2 4
3 4
5 1000
5
1 1
2 2
3 3
4 4
5 5
1
官方题解:
我们可以将一条线段[xi,yi]分为两个端点xi和(yi)+1,
在xi时该点会新加入一条线段,同样的,在(yi)+1时该点会减少一条线段,
因此对于2n个端点进行排序,令xi为价值1,yi为价值-1,问题转化成了最大区间和,
因为1一定在-1之前,因此问题变成最大前缀和,我们寻找最大值就是答案,另外的,
这题可以用离散化后线段树来做。复杂度为排序的复杂度即nlgn,
另外如果用第一种做法数组应是2n,而不是n,由于各种非确定性因素我在小数据就已
经设了n=10W的点。
题解:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
pair<int,int>pa[MAXN<<];
int main(){
int T,N;
scanf("%d",&T);
while(T--){int a,b;
scanf("%d",&N);
for(int i=;i<N;i++){
scanf("%d%d",&a,&b);
pa[i<<]=make_pair(a,);
pa[i<<|]=make_pair(b+,-);
}
sort(pa,pa+N*);
int ans=,cnt=;
for(int i=;i<*N;i++)
cnt+=pa[i].second,ans=max(ans,cnt);
printf("%d\n",ans);
}
return ;
}
lines(最大区间和)的更多相关文章
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
- 【线段树区间合并】HDU1540-Tunnel Warfare
一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU 4293---Groups(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- hdu3437 划分树 区间内小于第K大的值得和
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
- HDU 1698 Just a Hook(线段树/区间更新)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description In the g ...
随机推荐
- SASS组件开发
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- iOS 百度地图大头针使用
百度地图使用第五讲:大头针使用(地图标注)http://bbs.yusian.com/thread-8384-1-1.html(出处: 小龙虾IT笔记)
- Objective-c 中的变量
OC中的语言变量,按作用域可分为两种:局部变量和全局变量. 局部变量:也称为内部变量,局部变量是在方法内部声明的.其作用域仅限于方法内,离开该方法再使用这个变量就是非法的. 全局变量:也称为外部变量, ...
- ExtJs006类别名、备用名
Ext.onReady(function () { //Ext.define 其他配置项 //别名.备用名 Ext.define("User", { config: { name: ...
- 表单验证插件 jquery.validata 使用方法
参考资料:http://www.runoob.com/jquery/jquery-plugin-validate.html 下载地址 jquery.validate插件的文档地址http://docs ...
- WTL error C3861: 'DDX_Control': identifier not found
error C3861: 'DDX_Control': identifier not found 继承类加上 public CWinDataExchange<CMainDlg>, ...
- jquery的extend()函数
extend()是在写插件的过程中常用的方法,该方法有一些重载原型. 1.该方法的原型是: extend(dest,src1,src2,src3...); 它的含义是将src1,src2,src3.. ...
- 采用translate实现垂直水平居中
今天分享一个利用css3新特性实现垂直水平居中的方法. 通过对元素进行绝对定位再配合transform中的translate实现. 代码如下: html <div id="conten ...
- pexpect-pxssh-登陆Linux-执行命令
#!/usr/bin/python import pexpect import pxssh try: remote=pxssh.pxssh() hostname=raw_input('hostname ...
- selenium 学习笔记 ---新手学习记录(9) 问题总结(java)
1.百度搜索目标--实例 boolean flag; //点击次数 int shcs=3; int i=0; //窗口焦点控制 String detailWindowHandle = null; // ...