I want a priority queue such that first it should be done on basis of first element (increasing order)and when clash occurs then on basis of second element(decreasing order). I came up with the following code :
#include<bits/stdc++.h>
using namespace std;
struct Compare {
constexpr bool operator()(pair<int, int> const & a,
pair<int, int> const & b) const noexcept
{ return a.first > b.first && a.second<b.second; }
};
int main()
{
priority_queue<pair<int,int>,
std::vector<pair<int,int> >,
Compare> Q;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
Q.push(make_pair(a,b));
}
for(ll i=0;i<n;i++)
{
pair<ll,ll> m;
m=Q.top();
cout<<m.first<<" "<<m.second<<"\n";
Q.pop();
}
}
For eg - If I have following pairs : (1,10),(1,7),(1,5),(2,8),(2,6),(3,5),(3,9)
My output should be as follows on calling Q.top() and Q.pop()
1 10
1 7
1 5
2 6
2 8
3 5
3 9
But instead I'm getting the following output:
1 10
1 5
3 9
1 7
2 8
2 6
3 5
Can someone help me out ?