zoj 1076 Gene Assembly 贪心
这题就是任务调度,给定任务的起始和结束时间,求能完成任务的最大数目。
贪心,先按任务的结束时间递减排序,再逐个选择。选择活动的起始时间大于已选择活动的结束时间
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int N = 50005; struct info { int start, end; int id; }; info a[N]; int n; bool cmp(const info &a, const info &b) { return a.end < b.end; } int main() { int n; while (scanf("%d", &n) && n) { for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].start, &a[i].end); a[i].id = i+1; } sort(a, a+n, cmp); vector<int> ans; ans.push_back(0); for (int i = 1; i < n; i++) { if (a[ ans[ans.size()-1] ].end <= a[i].start) ans.push_back(i); } for (int i = 0; i < ans.size()-1; i++) cout << a[ans[i]].id << " "; cout << a[ ans[ans.size()-1] ].id << endl; } return 0; }