( 这个题数据量比较小,所以类似于模拟也可以做。)
按照起始时间排序,然后从最小的起点开始,找在这一天内能够观看的所有节目,写了个二分优化了时间。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 110
using namespace std;
typedef struct node
{
int s,e;
bool operator <(const node &th) const
{
if(th.s==s)
return th.e>e;
return th.s>s;
}
} node;
node timee[MAXN];
int n;
bool vis[MAXN];
int found(int x,int v)
{
if(x>=n) return x;
int y=n;
while(x<y)
{
int m=x+(y-x)/2;
if(timee[m].s>=v)
y=m;
else x=m+1;
}
return x;
}
int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)&&n)
{
for(int i=0; i<n; i++)
{
scanf("%d%d",&timee[i].s,&timee[i].e);
}
sort(timee,timee+n);
int ans=0;
memset(vis,false,sizeof(vis));
for(int i=0; i<n; i++)
{
if(vis[i]) continue;
int x=i;
while(x<n)
{
vis[x]=true;
int temp=x;
x=found(x+1,timee[temp].e+1);
while(vis[x])
x=found(x+1,timee[temp].e+1);
}
ans++;
}
printf("%d\n",ans);
}
return 0;
}