zoukankan      html  css  js  c++  java
  • POJ 3190 Stall Reservations

    题目链接:POJ 3190 Stall Reservations

    题目大意:

    题解:
    先按开始时间从小到大排序,然后用优先队列按结束时间从小到大的顺序去存储。
    每次从优先队列中选择结束时间最小的牛,并与当前将要存入优先队列的牛作比较,判断是否可以使用同一台机器。

    #include <algorithm>
    #include <iostream>
    #include <queue>
    using namespace std;
    
    int machine[1000010], n;  // 存机器的编号
    struct Cow {
        int start, end, id;
        bool operator<(const Cow &obj) const {
            if (start == obj.start) {
                return end < obj.end;
            } else {
                return start < obj.start;
            }
        }
    } cow[1000010], temp;
    struct cmp {
        bool operator()(Cow &a, Cow &b) { return a.end > b.end; }
    };
    priority_queue<Cow, vector<Cow>, cmp> pq;
    
    int main() {
        ios::sync_with_stdio(false);
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            cin >> cow[i].start >> cow[i].end;
            cow[i].id = i;
        }
        sort(cow + 1, cow + 1 + n);
        pq.push(cow[1]);
        int ans = 1;
        machine[cow[1].id] = 1;
        for (int i = 2; i <= n; ++i) {
            if (!pq.empty()) {
                temp = pq.top();
                if (temp.end < cow[i].start) { // 继续用这台机器
                    machine[cow[i].id] = machine[temp.id];
                    pq.pop();
                } else { // 用一台新机器
                    ans++;
                    machine[cow[i].id] = ans;
                }
            }
            pq.push(cow[i]);
        }
        cout << ans << endl;
        for (int i = 1; i <= n; ++i) {
            cout << machine[i] << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    阅读第十到十二章有感
    程序测试学习之5.2作业
    作业五——封装
    作业4 阅读《构建之法》第6 第7章有感
    汉堡包~~~
    作业3 阅读《构建之法》1-5章
    结对子实验——小学生四则运算
    小学生四则运算程序
    学会提问(转)
    error of “omission” and “commission”
  • 原文地址:https://www.cnblogs.com/IzumiSagiri/p/15220699.html
Copyright © 2011-2022 走看看