自定义优先队列

 1 #include<stdio.h>
 2 #include<queue>
 3 using namespace std;
 4 
 5 
 6 struct node
 7 {
 8     friend bool operator< (node x,node y)
 9     {
10         return x.pre>y.pre;// > 表示从小到大,< 表示从大到小;根据pre来判优先度;
11     }
12     int pre;
13     int val;
14 };
15 
16 int main()
17 {
18     int i,j;
19     node a[1000];
20     int n;
21     priority_queue<node>q;
22     while(scanf("%d",&n)!=EOF)
23     {
24         node z;
25         for(i=0;i<n;i++)
26         {
27             scanf("%d%d",&a[i].pre,&a[i].val);
28             q.push(a[i]);
29         }
30         while(!q.empty())
31         {
32             node x;
33             x=q.top();
34             printf("%d %d\n",x.pre,x.val);
35             q.pop();
36         }
37         
38         printf("\n");
39     }
40 }
View Code

hdu1896

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 struct node
 6 {
 7     int place;
 8     int len;
 9     friend bool operator< (node a,node b)
10     {
11         if(a.place!=b.place)
12             return a.place>b.place;
13         else return a.len>b.len;
14     }
15 };
16 int main()
17 {
18     priority_queue<node>q;
19     priority_queue<node>p;
20     int i,j,t,n;
21     node s;
22     scanf("%d",&t);
23     while(t--)
24     {
25         scanf("%d",&n);
26         for(i=0;i<n;i++)
27         {
28             scanf("%d%d",&s.place,&s.len);
29             q.push(s);
30         }
31         int odd=1;
32         while(!q.empty())
33         {
34             s=q.top();
35             q.pop();
36             
37             if(odd)
38             {
39                 s.place+=s.len;
40                 q.push(s);
41             }
42             odd=!odd;
43         }
44         printf("%d\n",s.place);
45     /*    while(scanf("%d",&n)!=EOF)
46         {
47             for(i=0;i<n;i++)
48             {
49                 scanf("%d",&s.place);
50                 p.push(s);
51             }
52             while(!p.empty())
53             {
54                 s=p.top();
55                 p.pop();
56                 printf("%d ",s.place);
57             }
58             printf("\n");
59         }*/
60     }
61 }
View Code

 

posted @ 2015-06-07 14:04  sweat123  阅读(190)  评论(0编辑  收藏  举报