http://acm.hdu.edu.cn/showproblem.php?pid=1509
裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的值
注意的是优先级一样的时候输出顺序在前的
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 struct point { 6 int val,odr,num; 7 char na[101]; 8 bool operator <(const point & q)const 9 { 10 if (odr==q.odr) return num>q.num; 11 else return odr>q.odr; 12 } 13 }; 14 int main() 15 { 16 char lsy[5]; 17 int k=0; 18 point temp; 19 priority_queue<point>que; 20 while (~scanf("%s",lsy)) 21 { 22 if (strcmp(lsy,"GET")==0) 23 { 24 if (que.size()!=0) 25 { 26 temp=que.top(); 27 que.pop(); 28 printf("%s %d\n",temp.na,temp.val); 29 } 30 else 31 printf("EMPTY QUEUE!\n"); 32 } 33 else 34 { 35 scanf("%s %d %d",temp.na,&temp.val,&temp.odr); 36 temp.num=k++; 37 que.push(temp); 38 } 39 } 40 return 0; 41 }
http://acm.hdu.edu.cn/showproblem.php?pid=1873
也很简单的优先队列,有三个医生的,每个医生又自己的顺序,所以可以建立三个队列
1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 struct point { 6 int x,num; 7 bool operator <(const point & q)const 8 { 9 if (x==q.x) return num>q.num; 10 else return x<q.x; 11 } 12 }; 13 int main() 14 { 15 int t,x,y; 16 char lsy[5]; 17 priority_queue<point> a,b,c; 18 point temp; 19 while (~scanf("%d",&t)) 20 { 21 int k=1; 22 while (a.size()!=0) 23 a.pop(); 24 while (b.size()!=0) 25 b.pop(); 26 while (c.size()!=0) 27 c.pop(); 28 while (t--) 29 { 30 scanf("%s",lsy); 31 if (strcmp(lsy,"IN")==0) 32 { 33 scanf("%d %d",&x,&y); 34 temp.num=k++; 35 temp.x=y; 36 if (x==1) a.push(temp); 37 else if (x==2) b.push(temp); 38 else c.push(temp); 39 } 40 else 41 { 42 scanf("%d",&x); 43 if (x==1) 44 { 45 if (a.size()==0) 46 printf("EMPTY\n"); 47 else 48 { 49 temp=a.top(),a.pop(); 50 printf("%d\n",temp.num); 51 } 52 } 53 else if (x==2) 54 { 55 if (b.size()==0) 56 printf("EMPTY\n"); 57 else 58 { 59 temp=b.top(),b.pop(); 60 printf("%d\n",temp.num); 61 } 62 } 63 else 64 { 65 if (c.size()==0) 66 printf("EMPTY\n"); 67 else 68 { 69 temp=c.top(),c.pop(); 70 printf("%d\n",temp.num); 71 } 72 } 73 } 74 } 75 } 76 return 0; 77 }
http://acm.hdu.edu.cn/showproblem.php?pid=1896
题意 遇到第奇数个的石头就把它往前扔规定的距离,遇到第偶数个的石头就不动它 问最远的石头据出发点(起点)的距离
优先队列的应用,想到优先队列就很好解决了
1 #include<cstdio> 2 #include<queue> 3 using namespace std; 4 struct point { 5 int x,y; 6 bool operator <(const point & q) const 7 { 8 if (x==q.x) return y>q.y; 9 else return x>q.x; 10 } 11 }; 12 int main() 13 { 14 int t,n; 15 while (~scanf("%d",&t)) 16 { 17 while (t--) 18 { 19 int k=1; 20 priority_queue<point> que; 21 point temp; 22 scanf("%d",&n); 23 while (n--) 24 { 25 scanf("%d %d",&temp.x,&temp.y); 26 que.push(temp); 27 } 28 while (!que.empty()) 29 { 30 temp=que.top(),que.pop(); 31 if (k%2==1) 32 { 33 temp.x+=temp.y; 34 que.push(temp); 35 } 36 k++; 37 } 38 printf("%d\n",temp.x); 39 } 40 } 41 return 0; 42 }
时间: 2024-10-10 20:02:26