hdu 5124 點的最多覆蓋線段數+離散化

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

求x軸上被線段覆蓋最多次的點的線段覆蓋次數

輸入的左右端點範圍有10^9,離散化一下即可

有點像cf上某題的變種

[xi,yi]分爲兩個端點xi和(yi)+1,在xi時該點會新加入一條線段,同樣的,在(yi)+1時該點會減少一條線段,對每條線段,使xi++,yi--,最後將所有出現的端點排序後求最大前綴和即可。

<span style="font-size:14px;">#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int inf = 1000000000;
const int maxn = 2e5 + 5;
map<int,int > hash;
#define x first
#define y second
typedef pair<int,int> p2;

p2 s[maxn];
int a[maxn];
int work()
{
    int n,x0,y0,cnt_a = 0;
    RD(n);
    for(int i = 0;i < n;++i){
        RD2(s[i].x,s[i].y);
        s[i].y++;
        a[cnt_a++] = s[i].x;
        a[cnt_a++] = s[i].y;
    }
    int ans = 1;
    sort(a,a+cnt_a);
    int m = 1;
    hash[a[0]] = m++;
    for(int i = 1;i < cnt_a;++i){
        if(a[i] != a[i - 1])
            hash[a[i]] = m++;
    }
    clr0(a);
    for(int i = 0;i < n;++i){
        a[hash[s[i].x]]++,a[hash[s[i].y]]--;
    }
    int res = 0;
    for(int i = 0;i < cnt_a;++i){
        res+= a[i];
        ans = max(ans,res);
    }
    return ans;
}
int main()
{
    int _;
    RD(_);
    while(_--){
        printf("%d\n",work());
    }
    return 0;
}
</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章