BZOJ 1216 HNOI 2003 操作系统 堆

来源:互联网 发布:那英人品知乎 编辑:程序博客网 时间:2024/06/14 22:31

题目大意

给出一个CPU处理事件的规则,给出一些事件,问处理这些事件的顺序和结束时间。

思路

我们只需要维护一个堆来模拟他说的规则,之后按顺序输出就行了。

CODE

#define _CRT_SECURE_NO_WARNINGS#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 1510000using 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;}
0 0
原创粉丝点击