• 随手练——HDU-2037 、P-2920 时间安排(贪心)


    • 普通时间安排

    HDU-2037 :http://acm.hdu.edu.cn/showproblem.php?pid=2037

    选取结束时间早的策略。

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    class T {
    public:
        int start, end;
        T(int s, int e) {
            start = s; end = e;
        }
        bool operator<(T t)const {
            return end < t.end;
        }
    };
    
    int main() {
        int n, c1, c2;
        vector<T>v;
        while (cin >> n) {
            if (n == 0)break;
            while (n--) {
                cin >> c1 >> c2;
                v.push_back(T(c1, c2));
            }
    
            sort(v.begin(), v.end());
            int res = 1; int end = v[0].end;
            for (int i = 1; i < v.size(); i++) {
                if (v[i].start >= end) {
                    res++;
                    end = v[i].end;
                }
            }
            cout << res << endl;
            v.clear();
        }
        return 0;
    }
    •  时间管理升级1

    洛谷:https://www.luogu.org/problemnew/show/P2920

    思想和第一个差不多,稍微绕了一点小弯,这题装 vector 再做就超时了,能简尽量不要复杂。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    class TIME {
    public:
        int t_s, t_e;
    }T[1001];
    int cmp(TIME t1, TIME t2) {
        return t1.t_e < t2.t_e ? 1 : 0;
    }
    int main() {
        int N;
        cin >> N;
        for (int i = 0; i < N;i++) {
            cin >> T[i].t_s >> T[i].t_e;
            T[i].t_s = T[i].t_e - T[i].t_s;
        }
        sort(T, T + N, cmp);
            int i = 1;
            while (i < N) {
                if (T[i].t_s < T[i - 1].t_e) {
                    while (T[i].t_s < T[i - 1].t_e) {
                        T[i].t_s++;
                        T[0].t_s--;
                    }
                    if (T[0].t_s < 0)
                    break;
                }    
                else {
                    while (T[i].t_s != T[i-1].t_e) {
                        T[i].t_s--;
                        T[i].t_e--;
                    }
                    i++;
                }
            }
            if (T[0].t_s < 0 || i != N)cout << -1 << endl;
            else cout << T[0].t_s << endl;    
        return 0;
    }
  • 相关阅读:
    第02组 Alpha冲刺 (6/6)
    面向对象分析设计考试复习【历年卷】
    第02组 Alpha冲刺 (5/6)
    第02组 Alpha冲刺 (4/6)
    第02组 Alpha冲刺 (3/6)
    第02组 Alpha冲刺 (2/6)
    第02组 Alpha冲刺 (1/6)
    linux内核结构
    从别人的博客学习
    递归的认识
  • 原文地址:https://www.cnblogs.com/czc1999/p/10356830.html
Copyright © 2020-2023  润新知