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;
}

你可能感兴趣的:(堆,bzoj,HNOI2003)