bestcoder lines

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(1T100)(the data for N>100 less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1N105),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1XiYi109),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

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <algorithm>
using namespace std;

const double EPS=1e-8;
const int MAXN=1000000+5;
int a[MAXN*2], b[MAXN*2];

struct node
{
	int star, en;
} len[MAXN];


int bs(int a[], int l, int r, int x)
{
	int mid = (l+r) / 2;

	while(a[mid] != x)
	{
		if(a[mid] > x) {
			r = mid;
		}
		else if(a[mid] < x) {
			l = mid;
		}

		mid = (l+r)/2;
	}

	return mid;
}
int main()
{
	int cases, n, i;

	scanf("%d", &cases);

	while(cases--)
	{
		int cnt = 0, maxx = 0;

		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));

		scanf("%d", &n);
		
		for(i = 0; i < n; i++)
		{
			scanf("%d %d", &len[i].star, &len[i].en);
		
			a[cnt++] = len[i].star;
			a[cnt++] = len[i].en;
		}

		sort(a, a+cnt);
		int p = 1;

		for(i = 1; i < cnt; i++)
		{
			if(a[i] != a[i-1])
				a[p++] = a[i];
		}

		cnt = p;

		for(i = 0; i < n; i++)
		{
			int mid = bs(a, 0, cnt, len[i].star);

			b[mid] += 1;

			mid = bs(a, 0, cnt, len[i].en);

			b[mid+1] += -1;
		}

		int ans = b[0];

		for(i = 1; i < cnt; i++)
		{
			b[i] = b[i-1]+b[i];

			if(b[i] > ans)
				ans = b[i];
		}

		printf("%d\n", ans);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值