http://acm.hdu.edu.cn/showproblem.php?pid=2037
先对结束时间进行排序,然后用开始时间和上一个节目的结束时间来比较。
要是对开始时间进行比较,用结束时间来比较就不好了,因为不知道每个节目的长度,只知道开始时间比较难操作。确实我现在水平也打不出来。
#include <iostream> using namespace std; int main() { int n; while(cin >> n && n != 0) { int ts[105]= {0}, te[105] = {0}; int i = 0; while(n--) { cin >> ts[i] >> te[i]; i++; } int k = 0, t = 0, num = i; //¶Ô½áβʱ¼ä½øÐÐÅÅÐò for(k = 0; k < num - 1; k++) for(t = 0; t < num - 1 - k; t++) { if(te[t] > te[t + 1]) { int e = ts[t]; ts[t] = ts[t + 1]; ts[t + 1] = e; int w = te[t]; te[t] = te[t + 1]; te[t + 1] = w; } } int count = 1, b = te[0]; for(int y = 0; y < num; y++) { for(int c = y + 1; c < num; c++) { if(ts[c] >= b) { y = c - 1; b = te[c]; count++; break; } } } cout << count << endl; } }