随手练——HDU-2037 时间安排(贪心)

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;
}

猜你喜欢

转载自www.cnblogs.com/czc1999/p/10356830.html