poj 1716 Integer Intervals

        
//贪心,按区间的终止点(右边的点)从小到大排列,我们每次取区间最右边的点,使这个区间内至少有两个点在s中
#include <iostream>
#include
<algorithm>
using namespace std;
struct node
{
int x;int y;
bool operator<(const node& n)const
{
return y<n.y;
}
}interval[
10001];
int res[10001];

int main()
{
int n,i,j,ans,top=-1;
cin
>>n;
for(i=0;i<n;i++)
cin
>>interval[i].x>>interval[i].y;
sort(interval,interval
+n);
for(i=0;i<n;i++)
{
ans
=0;
for(j=0;j<=top;j++)
if(res[j]>=interval[i].x&&res[j]<=interval[i].y)
{
ans
++;
if(ans>=2)
break;
}
if(ans==0)
{
res[
++top]=interval[i].y-1;
res[
++top]=interval[i].y;
}
else if(ans==1)
res[
++top]=interval[i].y;
}
cout
<<top+1<<endl;
return 0;
}

  

posted on 2011-07-18 11:29  sysu_mjc  阅读(124)  评论(0编辑  收藏  举报

导航