阅读109 返回首页    go 技术社区[云栖]


简单哈希-hdoj-1425-sort

hdoj-1425-sort

Problem Description

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

Input

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

Output

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

Sample Input

5 3

3 -35 92 213 -644

Sample Output

213 92 3

//hd-1425-ac
#include<iostream>
# define offset 500000
bool hash[offset+500001];
int main(){
    int n,m,temp;
    while(scanf("%d%d",&n,&m)!=EOF){
          memset(hash,false,sizeof(hash));
          for(int i=0;i<n;i++){
                scanf("%d",&temp);
                hash[temp+offset]=true; 
          }
          for(int i=1000000;i>=0&&m>0;i--)
                if(hash[i]){
			    	if(m==1)	printf("%d\n",i-offset);
			    	else printf("%d ",i-offset);
			    	m--;
				}   
    }
    return 0;
}


最后更新:2017-04-03 12:56:12

  上一篇:go Cocos2dx入门小游戏---Runner教程
  下一篇:go vi 搜索命令 搜索字符串