hdu 1908 double queues

http://acm.hdu.edu.cn/showproblem.php?pid=1908

看到有两个优先级,然后题目中又有queue。。。就想到了优先队列。。。

但是优先队列的cmp函数没搞懂,因为比较的是结构体,好像要重载< 什么的。

然而并不会。

其实用map就可以做。。。

map在插入的时候可以自动按关键字排序,简直好评如潮!

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

using namespace std;
typedef long long LL;
const int inf = 8E8;



int cmd;
map<int,int>a;
int p,k;

int main()
{

    while (scanf("%d",&cmd)!=EOF&&cmd)
    {
       if (cmd==1)
       {
           scanf("%d %d",&k,&p);
           a[p]=k;
       }
       if (cmd==2)
       {
           if (a.empty())
           {
               cout<<"0"<<endl;
           }
           else
           {
               cout<<a.rbegin()->second<<endl;
               a.erase(a.find(a.rbegin()->first));
           }
       }
       if ( cmd==3 )
       {
           if (a.empty())
           {
               cout<<"0"<<endl;
           }
           else
           {
               cout<<a.begin()->second<<endl;
               a.erase(a.begin());
           }
       }
    }

    return 0;
}
原文地址:https://www.cnblogs.com/111qqz/p/4618278.html