Num 22:nyoj:0055 lazy Xiao Ming [priority queue]

Source: Internet
Author: User



In life we encounter all sorts of queues, and in C, there are several types of questions about queues:

Normal queue. Chain queue. Loop queue. Priority Queue (The most widely used algorithm, often appear in the BFS );

Let's summarize some of the usage of the priority queue here today:



Priority queue:


for the general queue, the following is a simple FIFO (first output), the principle of [FIFO];

This is the exact opposite of the stack's characteristics;

For priority queues, features are not FIFO, but elements that meet certain criteria are first out of the team (for example, each time the largest element is out of the team).


first, the method of declaration:


the definition of the priority queue is contained in the header file: #include <queue>;


1. General method:


priority_queue<int>q; Default from large to small order out of the queue;

Priority_queue<int,vector<int>,greater<int> >q4; //Note that ">>" will be considered wrong, so it is separated by a space number, the minimum value first, from small to large out of the team output

Priority_queue<int,vector<int>,less<int> >q5; //maximum priority, from large to small output

2. Customize the priority level:

struct cmp{    operator bool () (int x, int y)    {        return x > y;//Small to large output     }};p Riority_queue<int, VECTOR&L t;int>, cmp>q;//definition method

3. Structure declaration method:
struct node{    int x, y;   BOOL operator < (Node A, node B)//From small to large output    {        return a.x > b.x;//x high Priority    }};p riority_queue<node>q ;//define Method

Second, the basic operation :


Push (x) presses the x into the end of the queue;

Empty () returns true if the queue is null;

Pop () delete the top element;

Size () returns the number of elements owned in the priority queue;

Top () returns the top element of the priority queue;

the general Top () function and the pop () function are used together;


Topic:



Lazy Xiao Ming time limit: theMs | Memory Limit:65535KB Difficulty:3
Describe
Xiao Ming wants to eat fruit, just as the orchard fruit is ripe. In the orchard, Xiaoming has beaten down all the fruits and divided them into different piles according to the different kinds of fruit. Xiao Ming decided to synthesize all the fruits into a pile. Because Xiao Ming is lazy, in order to save energy, Xiao Ming began to think of ideas:
Each time, Xiao Ming can combine the two piles of fruit and consume the same amount of energy as the sum of the weight of two of the fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total energy consumed by xiaoming during the merging of fruits equals the physical strength of each merger.
Because it will take a lot of effort to carry these fruits home, so Xiao Ming in the combination of fruit as much as possible to save energy. Assuming that each fruit has a weight of 1, and that the number of fruits and the number of each fruit is known, your task is to design a combination of the sequence, so that xiaoming consumes the least amount of energy, and output this minimum energy consumption value.
For example, there are 3 kinds of fruit, the number is 1,2,9. The 1 and 2 stacks can be merged first, and the new heap number is 3, which consumes 3 of the energy. Next, the new heap is merged with the original third heap, and a new heap is obtained, with a number of 12, which consumes 12 of the energy. So Xiaoming spent a total of energy =3+12=15. It can be shown that 15 is the minimum physical cost.
Input
The first line of input integer N (0<n<=10) indicates the number of test data groups. Next, each set of test data inputs consists of two lines, the first of which is an integer n (1<=n<=12000), which indicates the number of species of the fruit. The second line contains n integers, separated by spaces, and the first integer AI (1<=ai&lt;=20000) is the number of fruit of the first I.
Output
Each set of test data output includes one row, which contains only an integer, which is the minimum physical cost.
Sample input
13 1 2 9
Sample output
15


Analysis: Each time the smallest two and from the queue (Top (), pop ()), sum and then put back to the queue (push ());

There is only one element left in the queue (that is, the maximum value); Qing 0 queue; output;


AC Code:

#include <stdio.h> #include <string.h> #include <queue>using namespace Std;priority_queue<long Long Int,vector<long long Int>,greater<long long int> >q;int main () {int t,n,m,i; long long int l,x,y; scanf ("%d", &t); while (t--) {  scanf ("%d", &n);  for (i=0;i<n;i++)  {   scanf ("%d", &m);   Q.push (m);   }  l=0;  while (Q.size ()!=1)  {   x=q.top ();   Q.pop ();   Y=q.top ();   Q.pop ();   x+=y;   l+=x;   Q.push (x);  }  while (!q.empty ())//Focus:: Emptying the sequence   {   q.pop ();  }  printf ("%lld\n", L); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Num 22:nyoj:0055 lazy Xiao Ming [priority queue]

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.