lines

lines

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1375    Accepted Submission(s): 571


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
 
Source

 题意:给你n个区间覆盖x轴,问哪个点被覆盖的线段数多,是几条。

由于数据范围太大,所以离散化……

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define maxn 500008

struct node
{
    int op,num;
}P[maxn];

int cmp(node a, node b)
{
    return a.op < b.op;
}

int main()
{
    int t, n, x, y;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        int k = 0, j;

        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            P[k].op = x;
            P[k++].num = 1;
            P[k].op = y+1;
            P[k++].num = -1;
        }
        sort(P, P+k, cmp);
        int cnt = 0, Max = 0;

        for(int i = 0; i < k; )
        {
            j = i;
            while(P[j].op == P[i].op && j < k)
            {
                cnt += P[j].num;
                j++;
            }

            i = j;
            Max = max(Max, cnt);
        }
        printf("%d
", Max);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Tinamei/p/4948096.html