BZOJ 1216 HNOI 2003 操作系統 堆

題目大意

給出一個CPU處理事件的規則,給出一些事件,問處理這些事件的順序和結束時間。

思路

我們只需要維護一個堆來模擬他說的規則,之後按順序輸出就行了。

CODE

#define _CRT_SECURE_NO_WARNINGS

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1510000
using namespace std;

struct Complex{
    int no, arrive, lasting, priority;

    bool operator <(const Complex &a)const {
        if(priority == a.priority)
            return arrive > a.arrive;
        return priority < a.priority;
    }
    void Read() {
        scanf("%d%d%d", &arrive, &lasting, &priority);
    }
}src[MAX];
int cnt;

priority_queue<Complex> q;
int rest[MAX];

int main()
{
    int x;
    while(scanf("%d", &x) != EOF) {
        src[++cnt].no = x;
        src[cnt].Read();
        rest[src[cnt].no] = src[cnt].lasting;
    }

    q.push(src[1]);
    int now_time = src[1].arrive;
    for(int i = 2; i <= cnt; ++i) {
        while(!q.empty() && now_time + rest[q.top().no] <= src[i].arrive) {
            Complex temp = q.top();
            q.pop();
            now_time += rest[temp.no];
            printf("%d %d\n", temp.no, now_time);
        }
        if(!q.empty())
            rest[q.top().no] -= src[i].arrive - now_time;
        now_time = src[i].arrive;
        q.push(src[i]);
    }
    while(!q.empty()) {
        now_time += rest[q.top().no];
        printf("%d %d\n", q.top().no, now_time);
        q.pop();
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章