优先队列+pair

时间:2020-05-24 01:05:36   收藏:0   阅读:378

一、根据pair的first降序排序,second升序排序

技术分享图片
//#include<utility>
typedef pair<int,int> P;
struct cmp
{
    bool operator()(const P p1,const P p2)
    {
        if(p1.first==p2.first) return p1.second>p2.second;
        else return p1.first<p2.first;
    }
};
priority_queue < P, vector<P>, cmp > que;
View Code
priority_queue < int, vector<int>, greater<int> > que;默认升序
priority_queue < int > que;默认降序
 
二、根据struct的x降序排序,y升序排序
技术分享图片
1 struct p{
2     int x,y;
3 }aa[10];
4 bool cmp(p a,p b)
5 {
6     if(a.x==b.x) return a.y<=b.y;
7     return a.x>b.x;    
8 }
9 sort(aa,aa+n,cmp);
View Code

 

 

原文:https://www.cnblogs.com/xxxinnn/p/12945332.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!