线性排序-计数排序

 

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

//给你n个整数,请按从大到小的顺序输出前m大的数
//定义一个辅助数组,数组下标是原数组的数字, 

const  int MAXN = 1e6 + 10 ;
const  int RANGE = 5e5;

int arr[MAXN];
int number[MAXN];
 
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF){
        //数组清空
            (number,0,MAXN);    
        for(int i = 0; i < n;i++){
            scanf("%d",&arr[i]);
            number[arr[i] + RANGE] ++;
        }    
        
        int count = 0;
        for(int j = MAXN; j >= 0; j--){
            if(count < m && number[j] != 0){
                printf("%d ",j-RANGE);
                count++;
            }
        }
    } 
    return 0;
}

 

posted @ 2020-04-02 19:03  天凉好个秋秋  阅读(141)  评论(0)    收藏  举报