您的位置:首页 > 产品设计 > UI/UE

ZOJ 2724 Windows Message Queue(二叉堆||优先队列)

2015-10-27 19:42 573 查看


Windows Message Queue
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
2724

Appoint description:
System Crawler (2015-10-27)

Description

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will
do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message
from the message queue.

Input

There's only one test case in the input. Each line is a command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there're one string means the message name and two integer means the parameter and priority followed
by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.

Output

For each "GET" command, output the command getting from the message queue with the name and parameter in one line. If there's no message in the queue, output "EMPTY QUEUE!". There's no output for "PUT" command.

Sample Input

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET


Sample Output

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!


题意: 给入输入输出命令,输入的信息包括 姓名(name) ,数字(para) 和权值(pri)其中 权值小的优先;

分析:构造小根二叉堆,利用小根二叉堆就能解决,这一题还一个条件说是权值相同的先出现的优先,在比较函数中还加了两个条件来判断。

二叉堆代码:50ms

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 60000 + 10;
const int MAXS = 100;
typedef struct info
{
    char name[MAXS];
    int para;
    int pri;
    int t;
}info;
info p[MAX];
int heap[MAX];
int top,used;
int main()
{
    used = 0;
    top = 0;
    int cnt = 0;
    char s[MAXS];
    while(scanf("%s", s) != EOF)
    {
        if(strcmp(s,"GET") == 0)
        {
            if(top == 0)
                printf("EMPTY QUEUE!\n");
            else
            {
                printf("%s %d\n",p[heap[1]].name, p[heap[1]].para);
                int k = 1;
                heap[k] = heap[top--];
                while(k*2 <= top)
                {
                    int son = k *2 ;
                    if(son < top && p[heap[son + 1]].pri < p[heap[son]].pri)
                    {
                        son++;
                    }
                    if(p[heap[son]].pri < p[heap[k]].pri)
                    {
                        swap(heap[son],heap[k]);
                        k = son;
                    }
                    else
                        break;
                }
            }
        }
        else if(strcmp(s,"PUT") == 0)
        {
            scanf("%s%d%d", p[used].name, &p[used].para, &p[used].pri);
            p[used].t = cnt++;
            int k = ++top;
            heap[k] = used++;
            while(k > 1)
            {
                int f = k / 2;
                if(p[heap[f]].pri > p[heap[k]].pri)
                {
                    swap(heap[f], heap[k]);
                    k = f;
                }
                else
                    break;
            }
        }
    }
    return 0;
}


优先队列:60ms

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <limits.h>
using namespace std;
const int MAX = 60010;
const int MAXS = 50;
struct info
{
    char name[MAXS];
    int para;
    int pri;
    int t;
};
info p[MAX];
char s[MAX];
bool operator< (info a,info b)
{
    if(a.pri == b.pri)
        return a.t > b.t;
    return a.pri > b.pri;
}
priority_queue<info> q;
int main()
{
    while(q.size())
        q.pop();
    int ind = 0;
    while(scanf("%s", s) != EOF)
    {
        if(strcmp(s,"PUT") == 0)
        {
            scanf("%s %d %d", p[ind].name, &p[ind].para, &p[ind].pri);
            p[ind].t = ind;
            q.push(p[ind]);
            ind ++;
        }
        else if(strcmp(s,"GET") == 0)
        {
            if(q.size())
            {
                info temp = q.top();
                q.pop();
                printf("%s %d\n", temp.name,temp.para);
            }
            else
                printf("EMPTY QUEUE!\n");
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: