POJ

原题链接

题意:给定每头牛的吃草的开始时间和结束时间,求最少需要的畜栏数

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 5e4+10;
struct node{
    int st, ed, id;
    bool operator < (const node & a) const{
        return st < a.st;
    }
}p[N];
struct node1{
    int ed, id, id1;
    bool operator < (const node1 & a) const{
        return ed > a.ed;
    }
};
struct node2{
    int ans, id;
    bool operator < (const node2 & a) const{
        return id < a.id;
    }
};
int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
#endif //ONLINE_JUDGE
    int n, a, b, ans = 0;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d%d", &a, &b);
        p[i] = (node){a, b, i};
    }
    sort(p, p + n);
    priority_queue<node1> q;
    vector<node2> qq;
    ans++;
    q.push((node1){p[0].ed, ans, p[0].id});
    qq.push_back((node2){ans, q.top().id1});
    for(int i = 1; i < n; i++){
        if(p[i].st <= q.top().ed){
            ans++;
            q.push((node1){p[i].ed, ans, p[i].id});
            qq.push_back((node2){ans, p[i].id});
        }
        else{
            q.push((node1){p[i].ed, q.top().id, p[i].id});
            qq.push_back((node2){q.top().id, p[i].id});
            q.pop();
        }
    }
    printf("%d\n", ans);
    sort(qq.begin(), qq.end());
    for(int i = 0; i < qq.size(); i++){
        printf("%d\n", qq[i]);
    }
    return 0;
}

posted on 2019-04-05 19:43  坤sir  阅读(426)  评论(0编辑  收藏  举报

导航