#include <iostream> using namespace std; struct Time { int start; int end; }; Time times[101]; int dp[101]; void sortTimes(Time times[], int n) { // 直接插入排序 for (int i = 1; i <= n; i++) { Time t = times[i]; int j = i - 1; while (j >= 1 && times[j].start > t.start) { times[j + 1] = times[j]; j--; } j++; times[j] = t; } } int main() { int n; while (cin >> n && n != 0) { for (int i = 1; i <= n; i++) { cin >> times[i].start >> times[i].end; } sortTimes(times,n); //cout << endl; //for (int i = 1; i <= n; i++) //{ //cout<< times[i].start << " "<<times[i].end<<endl; //} // start 升序后,找到 start <= end 的最长序列长度 int longest = -1; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = 0; int sublongest = 0; for (int j = 1; j < i; j++) { if (times[j].end <= times[i].start) { sublongest = sublongest >= dp[j] ? sublongest : dp[j]; } } sublongest += 1; dp[i] = sublongest; longest = longest > dp[i] ? longest : dp[i]; } cout << longest << endl; } }
时间: 2024-10-20 16:01:55