Basic priority queue exercises: POJ1338 Uva136 and poj1338uva136

Source: Internet
Author: User

Basic priority queue exercises: POJ1338 Uva136 and poj1338uva136

I learned about stack, queue, and priority queue last night. I don't know much about the stack. The queue is basically OK. I am very happy with the priority queue. I wanted to continue with this operation this morning, but I followed Li Chen and listened to the computational ry = It was a little early to listen to the computational ry with my level, but at least I understood a lot and felt very happy. I didn't listen any more at 10 o'clock. After all, Alexander was getting deeper and deeper. So I will write down the question of the ugly number.

Question) ugly (POJ Version)

Train of Thought: the first is 1, put in the ascending order of priority queue (small first-out), then the first element of each departure, that is, the smallest, and then multiply by 2, 3, 5, respectively, to determine the weight, inbound set;

You can stop the process from the URL of the request to the URL of the request. POJ uses an iterator to traverse the set search from the beginning. In fact, if you create a parallel priority queue, you can also use the subscript. However, the priority queue cannot use the subscript, the iterator must also be used. (Can't containers associated with MS use subscript ?)

Put the code first:

// Uugly numbers # include <iostream> # include <vector> # include <queue> # include <set> using namespace std; typedef long ll; const int a [3] = {2, 3, 5}; int main () {priority_queue <ll, vector <ll>, greater <ll> pq; // REMEMBER! IT's ">", NOT "> ". there should be spaces between them. Otherwise, the interpretation is incorrect. In addition, the vector <ll> is of the base type, indicating that the queue is stored as a vector; comrades said they can also use a two-way queue [What is it = set <ll> s; // a set is used to store known ugly numbers, which can be used to determine the weight, in the POJ version, you can also find the nth pq. push (1); // 1 s. insert (1); // 1 indicates the input of the set. Note that the insert function is used. Because it is a set, it is automatically sorted after insertion, and no location parameter is required; when there is a location parameter, insert is inserted before the specified location for (int I = 1; I ++) {ll x = pq. top (); // gets the element of the first queue to exit, but this element has the highest priority rather than the first element to join the queue. Therefore, use top and frontpq for simple queue. pop (); // the first element of the team. if (I> 1500) {break;} for (int j = 0; j <3; j ++) {ll y = a [j] * x; if (! S. count (y) {// sets weight s. insert (y); pq. push (y) ;}}} int n; // from here, poj is used to find the nth cin> N; while (n) {set <ll> :: iterator it = s. begin (); // iterator pointer is set to start for (int I = 0; I <n-1; I ++) it ++; // locate the specified position; note that because the first element is 0, that is, the first element, should be added to the N-2 to find the n-1 position, is the n element cout <* it <"\ n "; // output cin> n;} return 0;} like a pointer ;}

Answer: 859963392

Today, all of our students are on holiday.

We also want to have a holiday, but we are still insisting.

Today, we suddenly realized that every day from AM to AM, we spent 15 hours a day in the data center.

This is our winter vacation. Maybe many OIers are like this. This is our life.

I'm glad that this kind of life is what I like, and I'm glad to learn a lot.

The Chinese New Year is approaching, and everyone is happy.

-- The Merchant does not know whether she is willing to die or hate the country.

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.