poj 2051 Argus(数据结构:优先权队列)

简介:
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8312   Accepted: 3701

Description

A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries over streams run continuously over a period of time and incrementally return new results as new data arrives. For example, a temperature detection system of a factory warehouse may run queries like the following. 
Query-1: "Every five minutes, retrieve the maximum temperature over the past five minutes." 
Query-2: "Return the average temperature measured on each floor over the past 10 minutes."

We have developed a Data Stream Management System called Argus, which processes the queries over the data streams. Users can register queries to the Argus. Argus will keep the queries running over the changing data and return the results to the corresponding user with the desired frequency. 

For the Argus, we use the following instruction to register a query: 
Register Q_num Period

Q_num (0 < Q_num <= 3000) is query ID-number, and Period (0 < Period <= 3000) is the interval between two consecutive returns of the result. After Period seconds of register, the result will be returned for the first time, and after that, the result will be returned every Period seconds. 

Here we have several different queries registered in Argus at once. It is confirmed that all the queries have different Q_num. Your task is to tell the first K queries to return the results. If two or more queries are to return the results at the same time, they will return the results one by one in the ascending order of Q_num.

Input

The first part of the input are the register instructions to Argus, one instruction per line. You can assume the number of the instructions will not exceed 1000, and all these instructions are executed at the same time. This part is ended with a line of "#". 

The second part is your task. This part contains only one line, which is one positive integer K (<= 10000).

Output

You should output the Q_num of the first K queries to return the results, one number per line.

Sample Input

Register 2004 200
Register 2005 300
#
5

Sample Output

2004
2005
2004
2004
2005

Source

题意理解:

(1)每个注册用户均有一个注册ID和一个时间间隔

(2)针对每隔用户,每隔一个自己的时间间隔该ID打印一次

(3)#说明输入到结尾处,没有用户注册了

(4)最后一行的数字为打印的次数

方法:这里使用了一个优先权队列,把所有的注册用户放入该队列中,队列的排序按照要出现的时间从小到大排序,如果时间有冲突就按照id升序。每次这个用户id输出后,先把该用户从队列里pop出来,然后把该用户的出现时间加上他的时间间隔,然后再push到优先权队列中,由于这个队列是按照要求排序的,所以每次在队头的那个元素就是要打印的ID

复制代码
#include <iostream>
#include <queue>
using namespace std;

//定义一个注册用户信息
typedef struct
{
    int id;//用户ID
    int time;//下一次出该出现的时间
    int period;//时间间隔
}Register;

//定义优先权队列的比较函数
struct cmp
{
    bool operator()(Register a,Register b)
    {
        if(a.time==b.time)
        return a.id>b.id;//时间相等时按照id升序排列
        return a.time>b.time;//按照时间升序排列,成为最小堆
    }
};

int main()
{
    //定义优先权队列,到达时间最小的最先到,相等时按照id升序排列
    priority_queue<Register,vector<Register>,cmp > re;
    string cmd;//命令
    int id;//定义id
    int period;//定义时间间隔
    int count;//定义输出条数
    while(true)
    {
        cin>>cmd;
        if(cmd.compare("#")==0)break;
        cin>>id>>period;
        Register r;
        r.id = id;
        r.period = period;
        r.time = period;
        re.push(r);
    }
    cin>>count;
    while(count--)
    {
        Register reg = re.top();
        cout<<reg.id<<endl;
        reg.time += reg.period;
        re.pop();
        re.push(reg);
    }
    return 0;
}
复制代码
本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/archive/2012/09/10/2678873.html  ,如需转载请自行联系原作者
相关文章
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3494 Largest Submatrix of All 1’s(单调栈)
POJ 3494 Largest Submatrix of All 1’s(单调栈)
|
1月前
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
186 9
|
1月前
|
存储 算法
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
32 1
|
23天前
|
存储 缓存 算法
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式,强调了合理选择数据结构的重要性,并通过案例分析展示了其在实际项目中的应用,旨在帮助读者提升编程能力。
44 5
|
1月前
|
存储 算法 Java
数据结构的栈
栈作为一种简单而高效的数据结构,在计算机科学和软件开发中有着广泛的应用。通过合理地使用栈,可以有效地解决许多与数据存储和操作相关的问题。
|
1月前
|
存储 JavaScript 前端开发
执行上下文和执行栈
执行上下文是JavaScript运行代码时的环境,每个执行上下文都有自己的变量对象、作用域链和this值。执行栈用于管理函数调用,每当调用一个函数,就会在栈中添加一个新的执行上下文。