Poj 2051 Argus (最小堆/优先队列)

来源:互联网 发布:aes算法c语言实现 编辑:程序博客网 时间:2024/05/21 08:49
Argus
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions:11624 Accepted: 5654

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.


最小堆(因为最近在补基础加一个书上板子吧。。。)


#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;#define N 10005struct Node{    int Q_num;    int Now;    int period;} a[N];void Swap(Node *a, Node *b){}void Down(Node H[], int s, int m){    Node rc = H[s];    int j;    for(int j = s * 2; j <= m; j *= 2)    {        if(j < m)///选取偏小的哪一个节点        {            if(H[j].Now > H[j+1].Now)            {                j ++;            }            else            {                if(H[j].Now == H[j + 1].Now && H[j].Q_num > H[j + 1].Q_num)                {                    j ++;                }            }        }        ///如果当前节点比子节点中最小的节点都小那么我们就结束调整过程        if(rc.Now < H[j].Now || (rc.Now == H[j].Now && rc.Q_num < H[j].Q_num))        {            break;        }        ///将这个节点与小的子节点替换。s 是 父节点 j 是子节点 将子节点的值放在父节点上。        H[s] = H[j];        s = j;    }    H[s] = rc;///最后调整位置是 s h存储上我们开始调整的值}void MakeMinHeep(Node H[], int len){    for(int i = len/2; i>0; -- i)///只需要将一半的点进行调整就好了,因为有一半的点是叶子节点不需要调整    {        Down(H, i, len);    }}int main(){    char op[105];    int i=0;    while(~scanf("%s", op))    {        if(strcmp(op,"#") == 0)        {            break;        }        i++;        scanf("%d%d", &a[i].Q_num, &a[i].period);        a[i].Now = a[i].period;    }    int len = i;    MakeMinHeep(a, i);    int k;    scanf("%d", &k);    for(int i = 0; i < k; i ++)    {        printf("%d\n", a[1].Q_num);        a[1].Now += a[1].period;        Down(a, 1, len);    }}


优先队列:


#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;struct Node{    int Q_num;    int period;    int Now;    bool operator < (const Node &a)const{        if(Now == a.Now){            return Q_num > a.Q_num;        }        return Now > a.Now;    }}x;int main(){    char op[15];    priority_queue<Node>q;    while(~scanf("%s", op)){        if(strcmp(op,"#") == 0)break;        scanf("%d%d", &x.Q_num, &x.Now);        x.period=x.Now;        q.push(x);    }    int k;    scanf("%d", &k);    for(int i=0;i<k;i++){        printf("%d\n",q.top().Q_num);        x=q.top();        x.Now += x.period;        q.pop();        q.push(x);    }}


STL   堆:


#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define N 1005struct Node{    int Q_num;    int period;    int Now;    bool operator < (const Node &a)const    {        if(Now == a.Now)        {            return Q_num > a.Q_num;        }        return Now > a.Now;    }} Heap[N];int main(){    char op[15];    int size=0;     while(scanf("%s",op),op[0]!='#'){        size++;        scanf("%d%d",&Heap[size].Q_num,&Heap[size].Now);        Heap[size].period=Heap[size].Now;        push_heap(Heap+1,Heap+size+1);    }    int k;    scanf("%d", &k);    for(int i=0; i<k; i++)    {        printf("%d\n",Heap[1].Q_num);        Heap[1].Now += Heap[1].period;        pop_heap(Heap+1,Heap+size+1);        push_heap(Heap + 1,Heap+size+1);    }}


原创粉丝点击