UVALive 3135 Argus(优先队列)

来源:互联网 发布:阿里云服务器怎样使用 编辑:程序博客网 时间:2024/06/06 03:23

题意:

有一系列的事件,它每Period秒钟就会产生编号为qNum的事件,你的任务是模拟出前k个事件,如果多个事件同时发生,先处理qNum小的事件。

思路:

用优先队列模拟。

AC代码

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int N = 105;struct Node {    int qnum, period, time;    //优先队列默认为最大堆    bool operator < (const Node& a) const {        if(time != a.time) return time > a.time;        else return qnum > a.qnum;    }};char buf[N];int main() {    priority_queue<Node> que;     Node node;    while(scanf("%s", buf) != EOF) {        if(buf[0] == '#') break;        scanf("%d%d", &node.qnum, &node.period);        node.time = node.period;        que.push(node);    }    int q;    scanf("%d", &q);    while(q--) {        Node top = que.top();        que.pop();        printf("%d\n", top.qnum);        top.time += top.period;        que.push(top);    }    return 0;}
0 0