原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037
CSUST链接: http://acmore.net:8080/contest/view.action?cid=2#problem/F
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
算法思想: 贪心。
思路:把每场电视剧按照先结束先看排序。
因为要保证看完的电视剧的场数最多 ,所以是尽量选短的开始看 ,也就是先结束的先看。自己画个图排序就明了了。
所以样例中的 5 场为:
1 3
3 4
5 10
15 20
//Accepted 2037 0MS 252K 840 B G++ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 105; struct Program{ int start; int end; }a[maxn]; bool cmp(Program a, Program b) /* 按照结束的顺序排序,先结束的排在前面 */ { if(a.end != b.end) return a.end < b.end; else return a.start >= b.start; } int main() { int n; while(scanf("%d", &n) != EOF) { int ans = 0; if(n == 0) break; for(int i = 0; i < n; i++) { scanf("%d%d", &a[i].start, &a[i].end); } sort(a, a+n, cmp); ans = 1; /*已经看了排序好的第一场电视剧 */ int time = a[0].end; /* 记录看完当前电视剧时 , 已经到达的时间 */ for(int i = 1; i < n; i++) { if(a[i].start >= time) /* 如果下一场开始的时间正好等于或者晚于看完当前电视剧的时间 */ { ans++; time = a[i].end; } } printf("%d\n", ans); } return 0; }