Nyoj 55 Xiao Ming, a lazy and easy-to-use person, [priority queue]

Source: Internet
Author: User

 

By default, the standard library uses the <operator of the element type to determine the priority relationship between them.

The first and most common usage of priority queue:

Priority_queue <int> qi;

The <operator indicates that the priority of a large element in an integer is high.

The output result in Example 1 is: 9 6 5 3 2


Method 2:

In example 1, what if we want to output elements from small to large?

In this case, we can pass in a comparison function using the functional. H function object as the comparison function.

Priority_queue <int, vector <int>, greater <int> qi2;

Where

The second parameter is the container type.

The second parameter is a comparison function.

The output result in example 2 is: 2 3 5 6 9


Method 3:

Custom priority.

Struct Node
{
Friend bool operator <(node N1, node N2)
{
Return n1.priority <n2.priority;
}
Int priority;
Int value;
};

In this structure, value is the value and priority is the priority.

You can use the custom operator <operator to compare the priority of an element.

In Example 3, the output result is:

Priority Value

9 5

8 2

6 1

2 3

1 4

However, if the structure is defined as follows:

Struct Node
{
Friend bool operator> (node N1, node N2)
{
Return n1.priority> n2.priority;
}
Int priority;
Int value;
};

It will not compile (G ++ compiler)

Because the standard library uses the <operator of the element type by default to determine the priority relationship between them.

The <operator and> operator of the custom type is not directly related, so it cannot be compiled.

// Code list

# Include <iostream>
# Include <functional>
# Include <queue>
Using namespace STD;
Struct Node
{
Friend bool operator <(node N1, node N2)
{
Return n1.priority <n2.priority;
}
Int priority;
Int value;
};
Int main ()
{
Const int Len = 5;
Int I;
Int A [Len] = {3, 5, 9, 6, 2 };
// Example 1
Priority_queue <int> qi;
For (I = 0; I <Len; I ++)
Qi. Push (A [I]);
For (I = 0; I <Len; I ++)
{
Cout <QI. Top () <"";
Qi. Pop ();
}
Cout <Endl;
// Example 2
Priority_queue <int, vector <int>, greater <int> qi2;
For (I = 0; I <Len; I ++)
Qi2.push (A [I]);
For (I = 0; I <Len; I ++)
{
Cout <qi2.top () <"";
Qi2.pop ();
}
Cout <Endl;
// Example 3
Priority_queue <node> Qn;
Node B [Len];
B [0]. Priority = 6; B [0]. value = 1;
B [1]. Priority = 9; B [1]. value = 5;
B [2]. Priority = 2; B [2]. value = 3;
B [3]. Priority = 8; B [3]. value = 2;
B [4]. Priority = 1; B [4]. value = 4;

For (I = 0; I <Len; I ++)
Qn. Push (B [I]);
Cout <"Priority" <'\ t' <"value" <Endl;
For (I = 0; I <Len; I ++)
{
Cout <Qn. Top (). Priority <'\ t' <Qn. Top (). value <Endl;
Qn. Pop ();
}
Return 0;
}

Nyoj 55 Xiao Ming

The idea is to take the minimum two heap each time and insert the result into the queue .. It cyclically knows that there are only a bunch of queues left...

Code ::

 #include<stdio.h>#include<functional>#include<queue>using namespace std;int main(){int a,b,n,m,k;scanf("%d",&n);while(n--){int sum=0,loop,tem;scanf("%d",&m);priority_queue<int, vector<int>, greater<int> >q;for(a=1;a<=m;a++){scanf("%d",&k);q.push(k);}while(q.size()!=1){loop=q.top();q.pop();tem=q.top();q.pop();b=loop+tem;sum+=b;q.push(b);}printf("%d\n",sum);}}        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.