"8.20 simulation" bubble sort

Source: Internet
Author: User

Title Description

given the n,k, and a sequence of length n, output the result of this sequence after the bubble sort K times.

Thinking of solving problems

We look at the pseudo-code given above, and we can see that this is a code that sorts the code into ascending order, so let's consider some of the characteristics of a counterfeit sort.

A large number to the right to exchange, but once the exchange can be changed a lot of positions, so do not know where to go to change once, so it is difficult to maintain.

A small number is exchanged at most and the larger number of its left one at a time, so at most once a position is run to the left, which is not known better than the large number of maintenance to go there.

Further thinking, after the K-Exchange, up to the left to run the K-step, that is to say, the number of k+1 position can run to 1 of the position up, then we can boldly guess, the first k+1 number of the number of the second after the exchange of the smallest will be the first position. If the smallest number of former k+1 does not run to the first position, it means that there must be a smaller number than it will be in its left to block it, and our assumptions contradict, so conjecture is established.

And so on, the number of k+2 is up to the second position, and according to our conclusion, the second position should be the smallest number except the number of the first position between 1 ~ k+2.

Then push down and we're done with all the numbers in the position of the calculation.

As can be seen, we need to maintain a minimum value, obviously can use the line segment tree, you can use the priority queue, note that the push to use a negative value.

Code
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <queue>6 using namespacestd;7 Const intmaxn=100050;8 intn,k;9 intA[MAXN];Tenpriority_queue<int>Q; One intMain () { Ascanf"%d%d",&n,&k); -      for(RegisterintI=1; i<=n;i++) scanf ("%d",&a[i]); -      for(RegisterintI=1; I<=min (n,k+1); i++){ theQ.push (-a[i]); -     } -      for(RegisterintI=2; i+k<=n;i++){ -printf"%d\n",-q.top ()); + Q.pop (); -Q.push (-a[i+K]); +     } A      while(!Q.empty ()) { atprintf"%d\n",-q.top ()); - Q.pop (); -     } -}

"8.20 simulation" bubble sort

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.