#include<cstdio>//¿Õ¼ä»»Ê±¼ä
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
int hash[1000010];
int main(){
while(~scanf("%d%d",&n,&m)){
memset(hash,0,sizeof(hash));
for(int i=0;i<n;i++){
int t;
scanf("%d",&t);
hash[t+500000]=1;
}
for(int i=1000000;i>=0;i--){
if(hash[i]==0)continue;
if(m==1){
printf("%d\n",i-500000);
break;
}
else {
printf("%d ",i-500000);
m--;
}
}
}
return 0;
}
题目意思:
给你n个整数,请按从大到小的顺序输出其中前m大的数。
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1425
分析:每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
用hash牺牲空间换取时间,达到常数级。