HDU 1509 Windows Message Queue

水题。用来熟悉优先队列。


有两个键,一个是优先级。一个是ID。按优先级排。优先级一样就按ID排。


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR0(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define debug puts("==fuck==")
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 20+10
using namespace std;
struct lx
{
    char str[101];
    int s;
    int value;
    int ID;
    friend bool operator <( lx a,lx b)
    {
        if(a.value==b.value)
            return a.ID>b.ID;
        return a.value>b.value;
    }
};
int main()
{
    priority_queue<lx> pq;

    char head[11];
    int Id=0;
    while(scanf("%s",head)!=EOF)
    {
        if(strcmp(head,"GET")==0)
        {
            if(pq.empty())
                puts("EMPTY QUEUE!");
            else
            {
                lx now;
                now=pq.top();
                pq.pop();
                printf("%s %d
",now.str,now.s);
            }
        }
        else if(strcmp(head,"PUT")==0)
        {
            lx now;
            scanf("%s%d%d",now.str,&now.s,&now.value);
            now.ID=Id++;
            pq.push(now);
        }
    }
}


原文地址:https://www.cnblogs.com/wgwyanfs/p/6795137.html