HDU 1425 sort 【哈希入门】

题意:给出n个数,输出前m大的数

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

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath>   
 5 #include<algorithm>  
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int M=500000;
10 int a,hash[M*2+5];
11 
12 
13 int main()
14 {
15     int n,m,i,j;
16     while(scanf("%d %d",&n,&m)!=EOF){
17         memset(hash,0,sizeof(hash));
18         for(i=1;i<=n;i++){
19             scanf("%d",&a);
20             hash[a+M]=1;//防止负数的下标 
21         }
22                         
23         for(i=2*M;m>0;i--){
24             if(hash[i]) {
25                 if(m!=1) printf("%d ",i-M);
26                 else printf("%d
",i-M);
27                 m--;
28             }            
29         }        
30     }
31     return 0;    
32 }
View Code

果断用sort超时了= =

原文地址:https://www.cnblogs.com/wuyuewoniu/p/4314634.html