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;
}
本文介绍了一种算法,用于计算在给定的水平线段中至少需要删除多少条线段以确保任意两条线段不重叠。该算法适用于不超过300,000条线段的情况,且所有线段具有相同的垂直坐标。
1071

被折叠的 条评论
为什么被折叠?



