Lines(HDU-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 contains a single integer T(1≤T≤100)(the data for N>100 less than 1 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
題意:t 組數據,每組給出 n 個區間?[xi,yi],求這些區間段所覆蓋的最多的點
思路:?對于一個區間 [xi,yi],將其分為兩個端點 xi、yi,在 xi 端點時該點會新加入一條新的線段,而對于 yi+1 的點,在該點時會減少一條線段,因此可以將給出的 2n 個端點進行排序,同時,令 xi 價值為 1,yi 價值為 -1,這樣一來,問題就由區間覆蓋轉換你為了最大區間和,由于 1 一定在 -1 之前,因此問題又轉換為了最大前綴和,尋找最大值即可
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-6 #define MOD 1000000007 #define INF 0x3f3f3f3f #define N 100001 #define LL long long using namespace std; int n; pair<int,int> a[N*2];//由于將區間轉為點,需要進行擴容 int main(){int t;scanf("%d",&t);while(t--){scanf("%d%",&n);n*=2;for(int i=1;i<=n;i++){scanf("%d",&a[i].first);//左端點xia[i].second=1;//左端點賦值i++;scanf("%d",&a[i].first);//右端點yia[i].first++;//右端點后移一位a[i].second=-1;//右端點賦值}sort(a+1,a+1+n);//默認對第一個元素升序排序int maxx=-INF;int sum=0;for(int i=1;i<=n;i++){sum+=a[i].second;//求前綴和maxx=max(maxx,sum);///記錄最大值}printf("%d\n",maxx);}return 0; }?
總結
以上是生活随笔為你收集整理的Lines(HDU-5124)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数列分块入门 6(LibreOj-628
- 下一篇: TDL(HDU-6641)