/* 现在有n项工作,知道每一项工作的开始时间和结束时间,问最多可以选择多少工作 算法设计:贪心算法,不断选择不冲突的那些结束时间最短的工作 */ #include<iostream> #include<vector> #define max_n 100001 using namespace std; int N; //first is the start of the job,and the secone is the end time of the job typedef pair<int, int> p; p vit[max_n]; int main() { while (cin >> N){ for (int i = 0; i < N; i++){ cin >> vit[i].first >> vit[i].second; } //sort according to the end time for (int i = 0; i < N - 1; i++){ for (int j = i + 1; j < N; j++){ if (vit[i].second>vit[j].second){ p t = vit[i]; vit[i] = vit[j]; vit[j] = t; } } } //end the sort,then start to choose the job int ans = 0,pre=0; for (int i = 0; i < N; i++){ if (pre < vit[i].first){ ans++; pre = vit[i].second; } } cout << ans << endl; } return 0; }
时间: 2024-10-11 03:47:01