hud 5124 lines(思维 + 离散化)

http://acm.hdu.edu.cn/showproblem.php?pid=5124

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(1N10^5),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1XiYi10^9),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
  
这道题与hdu1556做相似先看看hdu1556
 
题目大意: n个气球涂色,从编号a开始涂直到编号b,问每个气球被涂多少次
 
将起点x和终点y标记,然后再然后再处理标记之间的数
 
数据分析:
3 1 1 1 2 1 3
气球编号                    0    1    2    3    4
 第一次                       0    1    -1   0    0
 第二次                       0    2    -1   -1   0
 第三次                       0   3    -1   -1   -1
 最终                           0    3    2     1    0   
 
hdu 1556的代码:
 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define max(a, b)(a > b ? a : b)
#define N 100010

int c[N];
int main()
{
    int n, a, b, i;
    while(scanf("%d", &n), n)
    {
        memset(c, 0, sizeof(c));
        for(i = 1 ; i <= n ; i++)
        {
            scanf("%d%d", &a, &b);
            c[a]++;
            c[b + 1] -= 1;
        }
        for(i = 1 ; i <= n ; i++)
            c[i + 1] += c[i];
        printf("%d", c[1]);
        for(i = 2 ; i <= n ; i++)
            printf(" %d", c[i]);
        printf("
");
    }
    return 0;
}

在看这道题

题目大意:给一个线段涂色,n次操作,每次操作从点x涂到y,问最后涂得最多的那个点被涂了几次

与hdu 1556 做法相同,但是Xi 、 Yi(1XiYi10^9),数组没法开,所以我们需要想个办法来处理,就是离散化

我们将xi、yi都存在一个数组c里面,然后查找出xi和yi在数组c中出现的位置,用xi、yi各自的位置来代替xi、yi,再

来用上面的方法来写

lower_bound(c, c + 2 * n , a[i]) - c 这个函数返回的值是a[i]在数组c中第一次出现的位置

upper_bound(c, c + 2 * n , a[i]) - c 这个函数返回的值是a[i]在数组c中最后一次出现的位置

代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

const int N = 200010;
typedef long long ll;

int a[N], b[N], c[N], used[N];

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

int main()
{
    int t, n, p, q, x, y;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        memset(used, 0, sizeof(used));
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d%d", &p, &q);
            a[i] = p;
            b[i] = q;
        }
        for(int i = 0 ; i < n ; i++)
        {
            c[i] = a[i];
            c[i + n] = b[i];
        }
        qsort(c, 2 * n , sizeof(c[0]), cmp);
        for(int i = 0 ; i < n ; i++)
        {
            x = lower_bound(c, c + 2 * n , a[i]) - c;//这个函数用来查找a[i]在c中第一次出现的位置
            y = lower_bound(c, c + 2 * n , b[i]) - c;
            used[x]++;
            used[y + 1]--;
        }
        for(int i = 0 ; i < 2 * n ; i++)
            used[i+1] += used[i];
        qsort(used, 2 * n, sizeof(used[0]), cmp);
        printf("%d
", used[2 * n - 1]);
    }
    return 0;
}
 
 
原文地址:https://www.cnblogs.com/qq2424260747/p/4953349.html