hdu5124——lines

lines

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 330    Accepted Submission(s): 155

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

Recommend

heyang   |   We have carefully selected several similar problems for you:  5126 5125 5122 5121 5120

Statistic | Submit | Discuss
| Note

比赛时一开始用线段树WA了,然后树状数组过的,赛后听某牛说可以O(n),然后仔细想了下,比如现在要在[l,r]上覆盖一次,那么我们可以在s[l]上+1,s[r + 1] 上 - 1,记录覆盖次数,然后for一遍,当前的s[i] 加上 s[i - 1]就是i这个点被覆盖的次数,因为有s[r + 1] --,所以此方案是合理的,数据太大可以离散化一下

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int xis[N << 1];
int po[N << 1];

struct node
{
	int l, r;
}lines[N];

int main()
{
	int t, n, cnt, res, ans;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		map <int, int> newx;
		memset (xis, 0, sizeof(xis));
		newx.clear();
		cnt = 0;
		res = 0;
		for (int i = 0; i < n; ++i)
	 	{
		 	scanf("%d%d", &lines[i].l, &lines[i].r);
		 	po[cnt++] = lines[i].l;
		 	po[cnt++] = lines[i].r;
	 	}
	 	sort (po, po + cnt);
	 	for (int i = 0; i < cnt; ++i)
	 	{
	 		if (i == 0)
	 		{
	 			newx[po[i]] = ++res;
	 		}
	 		else if(po[i] == po[i - 1])
	 		{
	 			newx[po[i]] = newx[po[i - 1]];
	 		}
	 		else
	 		{
	 			newx[po[i]] = ++res;
	 		}
	 	}
	 	for (int i = 0; i < n; ++i)
	 	{
	 		int l = newx[lines[i].l];
	 		int r = newx[lines[i].r];
	 		xis[l]++;
	 		xis[r + 1]--;
	 	}
	 	ans = 0;
	 	for (int i = 1; i <= res; ++i)
	 	{
	 		xis[i] += xis[i - 1];
	 		if (ans < xis[i])
	 		{
	 			ans = xis[i];
	 		}
	 	}
	 	printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-11-08 21:42:18

hdu5124——lines的相关文章

HDU5124 lines

离散化 + 树状数组. 这些东西自己都是刚接触不久的,所以需要多写点题练练手. 题目描述: 一维坐标中有N条线段,其中有一个点上面覆盖的线段数是最多的,求该点上面的线段数目. 这道题和HDU1556特别相似,不过这道题数据比较大,所以要离散化预处理一下数据. 个人常用的离散化方法:先预存一下数据,然后用数组tmp[]存一下数据,对tmp[]数组排序,然后二分查找原数据在tmp[]数组中的下标,并且把下标作为离散化的数据. 然后就是树状数组这部分,一维树状数组支持两种操作: 1. 单点更新,区间求

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 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 conta

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

lines(最大区间和)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1350    Accepted Submission(s): 558 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

【POJ 3668】Game of Lines

Game of Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6555   Accepted: 2439 Description Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i ha

HDOJ 5031 Lines

枚举角度DFS.... Lines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 479    Accepted Submission(s): 140 Problem Description You play a game with your friend. He draws several lines on the paper wi

[POJ 1269]Intersecting Lines

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16311   Accepted: 7040 Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three

[Grid Layout] Describe a grid layout using named grid lines

We can use named grid lines to describe our grid layout. Let’s see how to apply this to our grid-template-columns and grid-template-rows, and how to refer to these from our grid items. Syntax like: grid-template-columns: [left-sidebar-start] 1fr [mai

rviz学习笔记(二)——Markers: Points and Lines (C++) 点和线

一.在using_marker/src中编写点和线代码 vim ~/catkin_ws/src/using_marker/src/points_and_lines.cpp 编写代码,其中有注释 #include <ros/ros.h> #include <visualization_msgs/Marker.h> #include <cmath> int main( int argc, char** argv ) { //创建一个发布器 ros::init(argc, a