You are not logged in. Please login at www.codechef.com to post your questions!

×

priority_queue c++

2
2

i want to make a min priority_queue of pair integers. but what i want to do is suppose first integer of one of the pair is 10 and second is 20 and that of a another pair first is 10 and second is also 10 i want that if two having equal value of first than than the one having more value of second should be popped first.

asked 21 Apr, 00:40

anno's gravatar image

4★anno
21310
accept rate: 10%

given input (10 20), (10 10), (2 10), (1 5), output should be (1 5), (2 10), (10 20), (10 10)

(21 Apr, 11:26) anno4★

link

answered 21 Apr, 11:28

dushsingh1995's gravatar image

4★dushsingh1995
7328
accept rate: 11%

its working .....but can you please explain the logic. That will be very helpful.

(21 Apr, 11:40) anno4★

yeah....I got it...Thank You

(21 Apr, 11:47) anno4★

Up vote me ... I am not able to ask questions

link

answered 21 Apr, 00:49

wanna_risk's gravatar image

0★wanna_risk
192
accept rate: 0%

You can perform this by using stl in c++.

EDIT : The default comparator for the priority queue is (l < r). You can see that in the constructor default parameter, By modifying the comparator in priority_queue you'd get the desired min priority queue.

bool operator() function compares the pairs. You can see that first, it will check whether the first element of pairs is equal or not if it's equal the constructor will do as you want.(Put the pair with a larger second on top().) if not then put the pair with larger first on top().

Here's the code

#include "bits/stdc++.h"
using namespace std;

struct compare
{
    bool operator()(pair<int, int> p1,pair<int,int> p2) {
        if(p1.first == p2.first)
            return p1.second < p2.second;
        else 
            return p1.first > p2.first;    // ***EDIT***
    }
};
int main()
{
    priority_queue<pair<int,int>,vector<pair<int,int>>,compare> pq;
    pq.push(make_pair(10,20));
    pq.push(make_pair(2,10));
    pq.push(make_pair(1,5));
    pq.push(make_pair(10,10));
    while(!pq.empty())
    {
        pair<int, int> w = pq.top();
        cout << w.first << " " << w.second << endl;;
        pq.pop();
    }
}

alt text

link

answered 21 Apr, 01:33

only4's gravatar image

4★only4
1.5k110
accept rate: 17%

edited 21 Apr, 17:11

this will sort only according to first element ....

(21 Apr, 07:29) marshal_roxx3★

no...its not working...it only sort according to the second element

(21 Apr, 10:52) anno4★

give input (10 20), (10 10), (2 10), (1 5), output should be (1 5), (2 10), (10 20), (10 10)

(21 Apr, 10:54) anno4★

can you explain how operator works

(21 Apr, 13:48) anno4★

Please read the edited answer.

(21 Apr, 22:08) only44★

guys, please upvote me. i am new here. nad not able to ask question

link

answered 21 Apr, 03:04

thinkinfinit's gravatar image

1★thinkinfinit
672
accept rate: 0%

Priority queue sorts only according to first elements. You can rather create a vector of pairs and sort function does this thing

link

answered 21 Apr, 00:48

wanna_risk's gravatar image

0★wanna_risk
192
accept rate: 0%

even sort function will sort according to first element

(21 Apr, 00:58) anno4★

No , sort function sorts a pairs in vector according to the two elements

(21 Apr, 07:41) wanna_risk0★
Answer is hidden as author is suspended. Click here to view.

answered 21 Apr, 07:48

marshal_roxx's gravatar image

3★marshal_roxx
(suspended)
accept rate: 2%

No..it will not work

(21 Apr, 10:32) anno4★

Bolanet88.net merupakan situs agen judi bola terpercaya yang menawarkan permainan judi online terlengkap. situs judi bola resmi agen taruhan bola indonesia bandar judi tangkas online uang asli daftar akun sbobet ibcbet

link

answered 21 Apr, 14:24

bolanet88's gravatar image

0★bolanet88
-1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,688
×4

question asked: 21 Apr, 00:40

question was seen: 1,273 times

last updated: 21 Apr, 22:08