2518: No overlap
--------------------------------------------------------------------------------
Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
1s 8192K 163 46 Standard
Give you N (1<=N<=300000) line segments, calculate at least remove how many line segments so that no line segments are overlaped. All line segments are horizontal and have a same vertical coordinate .
Input
There are several cases in the input, each case begin with a postive integer N, the following N lines each consists of two integers a, b (1 <= a < b <= 10000), which are the horizontal coordinate of the corresponding line segment.
Output
For each case output at least remove how many line segments so that no line segments are overlaped.
Sample Input
3
1 3
2 4
3 4
3
2 5
1 4
3 4
Sample Output
1
2
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
};
node a[300100];
bool cmp(node a,node b)
{
return a.y<b.y;
}
int main()
{
int n;
while(scanf("%d",&n)==1)
{
for(int i=0;i<n;i++) scanf("%d%d",&a[i],&a[i].y);
sort(a,a+n,cmp);
int cnt=1,i=0,j=1;
while(j<n)
{
if(a[j].x>=a[i].y)
{
cnt++;
i=j;
j++;
}
else j++;
}
printf("%d/n",n-cnt);
}
return 0;
}