qsort排序后DP,水题。注意,数组开大点儿,把时间理解为0~23,开太小会wa。
#include <stdio.h> #include <stdlib.h> #define MAXNUM 100 int comp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int mymax(int a, int b) { return (a>b) ? a:b; } int main() { int time[MAXNUM][2], n; int i, j, end; int num[MAXNUM]; while (scanf("%d", &n) != EOF && n) { end = 0; for (i=0; i<n; ++i) { scanf("%d %d", &time[i][0], &time[i][1]); if (time[i][1] > end) end = time[i][1]; } qsort(time, n, sizeof(int)*2, comp); memset(num, 0, sizeof(num)); for (i=0; i<n; ++i) { if (num[time[i][0]]+1 > num[time[i][1]]) { num[time[i][1]] = num[time[i][0]]+1; for (j=time[i][1]; j<=end; ++j) { num[j] = mymax(num[j], num[time[i][1]]); } } } printf("%d ", num[end]); } return 0; }