zoukankan      html  css  js  c++  java
  • 随手练——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;
    }
  • 相关阅读:
    linux初识1
    linux初识
    练习题
    linux 创建虚拟机常见错误
    DevGridControl中GridView排序问题
    小工具:火车票查询
    小工具:邮件发送
    小工具:截图&简单图像处理
    Winform 控件的入门级使用(一)
    Winform & Devexpress Chart使用入门
  • 原文地址:https://www.cnblogs.com/czc1999/p/10356830.html
Copyright © 2011-2022 走看看