题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1873
优先队列的一个应用,就是给数设置优先级,但是注意优先级的规则,不要漏掉任何一个。
代码如下:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define pf printf 7 #define mem(a,b) memset(a,b,sizeof(a)) 8 #define prime1 1e9+7 9 #define prime2 1e9+9 10 #define pi 3.14159265 11 #define lson l,mid,rt<<1 12 #define rson mid+1,r,rt<<1|1 13 #define scand(x) scanf("%llf",&x) 14 #define f(i,a,b) for(int i=a;i<=b;i++) 15 #define scan(a) scanf("%d",&a) 16 #define mp(a,b) make_pair((a),(b)) 17 #define P pair<int,int> 18 #define dbg(args) cout<<#args<<":"<<args<<endl; 19 #define inf 0x7ffffff 20 inline int read(){ 21 int ans=0,w=1; 22 char ch=getchar(); 23 while(!isdigit(ch)){if(ch==‘-‘)w=-1;ch=getchar();} 24 while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-‘0‘,ch=getchar(); 25 return ans*w; 26 } 27 int n,m,t; 28 const int maxn=4; 29 struct node{ 30 int num,id; 31 bool operator<(const node& a)const 32 { 33 if(id!=a.id)return id<a.id; 34 else return num>a.num;//编号相同时选择序号小的 35 } 36 node(int n,int i):num(n),id(i){} 37 node(){} 38 }; 39 int main() 40 { 41 //freopen("input.txt","r",stdin); 42 //freopen("output.txt","w",stdout); 43 std::ios::sync_with_stdio(false); 44 char s[10]; 45 int first=0; 46 while(~scanf("%d",&t)) 47 { 48 int k=0; 49 priority_queue<node> q[maxn]; 50 int a,b; 51 f(tt,1,t) 52 { 53 scanf("%s",s); 54 if(s[0]==‘I‘) 55 { 56 a=read(),b=read(); 57 q[a].push(node(++k,b)); 58 } 59 else if(s[0]==‘O‘) 60 { 61 a=read(); 62 if(!q[a].empty()) 63 pf("%d\n",q[a].top().num),q[a].pop(); 64 else pf("EMPTY\n"); 65 } 66 } 67 } 68 }
原文:https://www.cnblogs.com/randy-lo/p/12609485.html