题目11:Sort

来源:互联网 发布:网络测速器在线测网速 编辑:程序博客网 时间:2024/05/19 12:12

http://ac.jobdu.com/problem.php?cid=1040&pid=10

题目描述:

给你n个整数,请按从大到小的顺序输出其中前m大的数。

输入:

每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

输出:

对每组测试数据按从大到小的顺序输出前m大的数。

样例输入:
5 33 -35 92 213 -644
样例输出:
213 92 3
// 题目11:Sort.cpp: 主项目文件。#include "stdafx.h"#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <set>#include <map>#include <queue>#include <stack>using namespace std;const int N=1000003;bool hashTable[N];#define more 500000int main(){    //freopen("F:\\test.txt","r",stdin);    //freopen("F:\\output.txt","w",stdout);int n,m;while(scanf("%d%d",&n,&m)!=EOF){memset(hashTable,0,sizeof(hashTable));for(int i=0;i<n;i++){int tmp;cin>>tmp;hashTable[more+tmp]=true;}int cnt=0;bool flag=true;for(int i=N-1;i>=0;i--){if(hashTable[i]){cnt++;if(flag){flag=false;printf("%d",i-more);}elseprintf(" %d",i-more);}if(cnt==m)break;}printf("\n");}    return 0;}


原创粉丝点击