暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。
现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?
12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0
5
经典贪心,区间不相间问题:给出N个开区间,从中选取尽可能多的开区间,使其两两没有交集。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct Time{ 8 int x; 9 int y; 10 }T[100]; 11 12 bool cmp(Time a,Time b) 13 { 14 if(a.x!=b.x) 15 return a.x>b.x; 16 else return a.y<b.y; 17 } 18 19 int main() 20 { 21 int n; 22 while(~scanf("%d",&n)&&n) 23 { 24 for(int i=0;i<n;i++) 25 { 26 scanf("%d %d",&T[i].x,&T[i].y); 27 } 28 sort(T,T+n,cmp); 29 int num=1; 30 int lastx=T[0].x; 31 for(int i=1;i<n;i++) 32 { 33 if(T[i].y<=lastx) 34 { 35 num++; 36 lastx=T[i].x; 37 } 38 } 39 printf("%d\n",num); 40 41 } 42 return 0; 43 }
原文:https://www.cnblogs.com/jiamian/p/11165130.html