HDU-2037 :http://acm.hdu.edu.cn/showproblem.php?pid=2037
最基础的贪心题目,选取结束时间早的策略。
#include <iostream> #include <vector> #include <algorithm> using namespace std; class time { public: int t_s, t_e; time(int s,int e) { t_s = s; t_e = e; } }; int cmp(time t1,time t2) { return t1.t_e < t2.t_e ? 1 : 0; } int main() { int N; while (cin >> N) { vector<time>v; if (N == 0)break; while (N--) { int t_s, t_e; cin >> t_s >> t_e; v.push_back(time(t_s, t_e)); } sort(v.begin(), v.end(), cmp); vector<time>::iterator it = v.begin(); it++; while (it != v.end()) { if (it->t_s < (it-1)->t_e) { it = v.erase(it); } else it++; } cout << v.size() << endl; v.clear(); } return 0; }
原文地址:https://www.cnblogs.com/czc1999/p/10356830.html
时间: 2024-09-28 16:36:29