简单贪心,选择不相交区间:
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int n, s[25], e[25], r[25]; 5 6 int cmp(const void *_p, const void *_q) 7 { 8 int *p = (int *)_p; 9 int *q = (int *)_q; 10 return e[*p]-e[*q]; 11 } 12 13 int main() 14 { 15 while(scanf("%d",&n)) 16 { 17 if(n == 0) break; 18 for(int i = 0; i < n; i ++) 19 { 20 scanf("%d%d",&s[i],&e[i]); 21 r[i] = i; 22 } 23 qsort(r,n,sizeof(r[0]),cmp); 24 int ans = -1, count = 0; 25 for(int i = 0; i < n; i ++) 26 if(s[r[i]] >= ans){ans = e[r[i]]; count ++;} 27 printf("%d\n",count); 28 } 29 return 0; 30 }