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;
    }
    
  • 相关阅读:
    操作系统典型调度算法
    C++ volatile 关键字
    vue class绑定 组件
    yarn 基本用法
    vscode 插件安装以及首选项配置
    git 多人协作
    git Feature分支
    git Bug分支
    git 分支策略
    git 解决冲突
  • 原文地址:https://www.cnblogs.com/IzumiSagiri/p/15220699.html
Copyright © 2011-2022 走看看