閱讀109 返回首頁    go 阿裏雲 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 搜索命令 搜索字符串