HDU1873+优先队列

简单的优先队列。。。

View Code
 1 /*

 2 优先队列

 3 */

 4 #include<stdio.h>

 5 #include<string.h>

 6 #include<stdlib.h>

 7 #include<algorithm>

 8 #include<iostream>

 9 #include<queue>

10 #include<map>

11 #include<vector>

12 #include<math.h>

13 using namespace std;

14 typedef long long ll;

15 //typedef __int64 int64;

16 const int maxn = 105;

17 const int inf = 0x7FFFFFFF;

18 const int CLEAR = 0x7F;

19 const double pi=acos(-1.0);

20 const double eps = 1e-8;

21 

22 struct node{

23     int lev,ti;

24 };

25 

26 bool operator < ( node a,node b ){

27     if( a.lev!=b.lev ) return a.lev<b.lev;//现按照lev排序,lev大的在堆顶

28     else return a.ti>b.ti;//再按照timepaixu,小的在前

29 }

30 

31 priority_queue<node>q0;

32 priority_queue<node>q1;

33 priority_queue<node>q2;

34 node tmp;

35 void init(){

36     while( !q0.empty() ) q0.pop();

37     while( !q1.empty() ) q1.pop();

38     while( !q2.empty() ) q2.pop();

39 }

40 int main(){

41     int n;

42     while( scanf("%d",&n)!=EOF ){

43         init();

44         char op[ 5 ];

45         int a,b;

46         int cnt = 1;

47         while( n-- ){

48             scanf("%s",op);

49             if( op[0]=='I' ){

50                 scanf("%d%d",&a,&b);

51                 tmp.lev = b;

52                 tmp.ti = cnt++;

53                 if( a==1 ){

54                     q0.push( tmp );

55                 }

56                 else if( a==2 ){

57                     q1.push( tmp );

58                 }

59                 else {

60                     q2.push( tmp );

61                 }

62             }

63             else if( op[0]=='O' ){

64                 scanf("%d",&a);

65                 if( a==1 ){

66                     if( q0.empty() ) printf("EMPTY\n");

67                     else {

68                         node tt = q0.top();

69                         q0.pop();

70                         printf("%d\n",tt.ti);

71                     }

72                 }

73                 else if( a==2 ){

74                     if( q1.empty() ) printf("EMPTY\n");

75                     else {

76                         node tt = q1.top();

77                         q1.pop();

78                         printf("%d\n",tt.ti);

79                     }

80                 }

81                 else {

82                     if( q2.empty() ) printf("EMPTY\n");

83                     else{

84                         node tt = q2.top();

85                         q2.pop();

86                         printf("%d\n",tt.ti);

87                     }

88                 }

89             }

90         }

91     }

92     return 0;

93 }

 

你可能感兴趣的:(优先队列)