Looking Monopoly (25 points)

Report Research Institute survey showed that by the end of 2017, China more than 100 million yuan of personal assets of high net worth population of 15 million people. N hypothesis given individual's personal assets value, Monopoly quickly identify assets ranked first M bits.

Input formats:

First, given two input positive integers N ( . 1 0 . 6 ), and M ( . 1 0), where N is the total number, M is the number required to identify the Monopoly; given next line N individual personal asset value, $ million units, not to exceed the scope of a long integer integer. Between numbers separated by a space.

Output formats:

Assets in the front row output by the row non-increasing order of individual asset values of the M-bit Monopoly. Between numbers separated by spaces, not end with extra spaces.

Sample input:

8 3
8 12 7 3 20 9 5 18

Sample output:

20 18 12




Use C ++ library functions have been defined, for this question a little more useful.
Priority Queue
priority_queue <int, vector <int> , less <int >> s; // less insert elements represented in descending order (descending) of
priority_queue <int, vector <int> , greater <int >> s; // represents a greater insertion elements in ascending (small to large) order

Several of the more important functions

Basic operation:

empty () if the queue is empty, returns true

pop () to delete the top element, delete the first element

push () to one element

size () Returns the number of elements in the priority queue has

top () Returns the top element of the priority queue, returns the priority queue has the highest priority element

In the default priority queue, high priority, first-out team. In the default int type in, first-out team to a larger number.

code show as below:

#include<bits/stdc++.h>
#include<queue>
using namespace std;    
priority_queue<int,vector<int>,less<int> >x;
int main ()
{
    int i,l;
    int a,b;
    cin>>a;
    cin>>b;
    for(i=0;i<a;i++){
        cin>>l;
        x.push(l);
    }
    int c;
    if(a>b){
        for(l=0;l<b;l++){
            c=x.top();
            x.pop();
            if(l>0){
                cout<<" ";
            }
            cout<<c;
        }
    }
    else{
        for(l=0;l<a;l++){
            c=x.top();
            x.pop();
            if(l>0){
                cout<<" ";
            }
            cout<<c;
        }
    }
}

Guess you like

Origin www.cnblogs.com/zsw1244/p/11037774.html