原理不再讲了,在紫皮书232页。
#include<iostream>
#include<stdio.h>
#include <algorithm>
using namespace std;
struct time
{
int start,end;
};
int cmp(struct time a, struct time b)
{
return a.end < b.end;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF && n!=0)
{
struct time time_table[1000];
int cnt = 0;
for ( int i = 0; i < n; i++)
{
scanf("%d %d",&time_table[i].start, &time_table[i].end);
}
sort(time_table,time_table+n,cmp);
int bench = time_table[0].end;
for ( int i = 1; i < n; i++ )
{
if ( time_table[i].start >= bench )
{
cnt ++ ;
bench = time_table[i].end;
}
}
printf("%d\n", cnt);
}
}