POJ 2376 Cleaning Shifts

题目链接:POJ 2376 Cleaning Shifts

题目大意:

题解:
将所有牛按照开始时间排序,如果开始时间相同就按照结束时间大的在前。
如果第一头牛的开始时间不是1直接输出-1。
每次遍历一遍后面的牛,选择一只开始时间小于等于上一区间的结束时间并且结束时间尽可能大的牛。

#include <algorithm>
#include <iostream>
using namespace std;

struct cow {
    int start, end;
    bool operator<(const cow &obj) const {
        if (start == obj.start) {
            return end > obj.end;
        }
        return start < obj.start;
    }
} cow[250010];
int n, t;

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> t;
    for (int i = 1; i <= n; ++i) {
        cin >> cow[i].start >> cow[i].end;
    }
    sort(cow + 1, cow + n + 1);
    if (cow[1].start == 1) {
        if (cow[1].end == t) {
            cout << 1 << endl;
        } else {
            int now = 1, ans = 1;
            int start = cow[1].end + 1, end = cow[1].end;
            bool find = false;
            while (!find) {
                for (int i = now + 1; i <= n; ++i) {
                    if (cow[i].start <= start && cow[i].end > end) {
                        end = cow[i].end;
                        now = i;
                    }
                }
                if (start == end + 1) {
                    break;
                } else if (end == t) {
                    find = true;
                }
                ans++;
                start = end + 1;
            }
            if (find) {
                cout << ans << endl;
            } else {
                cout << -1 << endl;
            }
        }
    } else {
        cout << -1 << endl;
    }
    return 0;
}
posted @ 2021-09-02 20:25  ZZHHOOUU  阅读(27)  评论(0编辑  收藏  举报