Sdut 3401 Data Structure Experiment sort four: looking for the millionaire

Source: Internet
Author: User

The sort of data structure experiment four: Looking for a MillionaireTime limit:200 Ms Memory limit:512 KiB Problem Description

2015 Hurun Global Wealth list survey shows that personal assets in more than 10 million of high net worth people reach 2 million people, assuming that the personal value of the output of n individuals, please quickly find out the top m-bit monopoly.

Input

First enter two positive integers n (n≤10^6) and M (m≤10), where n is the total number of people, M is the number of millionaires that need to be found, next give N personal assets, in million units, the personal asset number is a positive integer, the number is separated by a space.

Output

A row of data, in descending order to output the assets of the monopoly of the millionaire's personal value, the numbers are separated by a space, there is no extra space at the end of the line.

Sample Input
6 312 6 56 23 188 60

Sample Output
188 60 56

Tip: The problem requires a heap row to solve, the heap needs to build a large heap and small heap. The time complexity of the heap is O (NLOGN)

The code is implemented as follows (GCC):
#include <stdio.h>inta[ -];intm;voidSwap (intXintY//Exchange Order{    intT; T=A[x]; A[X]=A[y]; A[y]=t;}voidSiftdown (intI//Look down{    intt,flag=0;//T is used to record smaller nodes and flag to determine if they can be adjusted downwards .     while(i*2<=m&&flag==0)    {        if(a[i]>a[i*2]) T=i*2; ElseT=i; if(i*2+1<=m) {if(a[t]>a[i*2+1]) T=i*2+1; }        if(t!=i) {Swap (t,i);//Exchangei=t;//Update down        }        ElseFlag=1;//Otherwise it can't be adjusted.    }}voidHeapsort ()//Heap Sort{     while(m>1) {Swap (M,1); M--;    Siftdown (m); }}intMain () {intnum,n,i,j; scanf ("%d%d",&num,&m);  for(i=1; i<=m;i++) {scanf ("%d",&A[i]); } N=m;  for(i=m/2; i>=1; i--) {Siftdown (i); }     for(i=m+1; i<=num;i++)    {        intx; scanf ("%d",&x); if(x>a[1]) {a[1]=x;  for(j=m/2; j>=1; j--) {Siftdown (j);    }}} heapsort ();  for(i=1; i<=n;i++) {i<n? printf"%d", A[i]):p rintf ("%d\n", A[i]); }    return 0;}/***************************************************result:acceptedtake Time:196mstake memory:156kb*********** *****************************************/

Sdut 3401 Data Structure Experiment sort four: looking for the millionaire

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.