zoukankan      html  css  js  c++  java
  • 自定义优先队列

     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
    ",x.pre,x.val);
    35             q.pop();
    36         }
    37         
    38         printf("
    ");
    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
    ",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("
    ");
    59         }*/
    60     }
    61 }
    View Code
  • 相关阅读:
    .NET XmlNavigator with Namespace
    编程要素
    【FOJ】1962 新击鼓传花游戏
    【POJ】1389 Area of Simple Polygons
    【POJ】2482 Stars in Your Window
    【HDU】3265 Posters
    【HDU】1199 Color the Ball
    【HDU】3642 Get The Treasury
    【HDU】4027 Can you answer these queries?
    【HDU】1542 Atlantis
  • 原文地址:https://www.cnblogs.com/sweat123/p/4558400.html
Copyright © 2011-2022 走看看