您的位置:首页 > 其它

HDU 1425 sort 【哈希入门】

2015-03-04 23:34 351 查看
题意:给出n个数,输出前m大的数

和上一题一样,将输入的数加上一个极大地值作为地址

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std;

typedef long long LL;
const int M=500000;
int a,hash[M*2+5];

int main()
{
int n,m,i,j;
while(scanf("%d %d",&n,&m)!=EOF){
memset(hash,0,sizeof(hash));
for(i=1;i<=n;i++){
scanf("%d",&a);
hash[a+M]=1;//防止负数的下标
}

for(i=2*M;m>0;i--){
if(hash[i]) {
if(m!=1) printf("%d ",i-M);
else printf("%d\n",i-M);
m--;
}
}
}
return 0;
}


View Code

果断用sort超时了= =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: