hdu1425

来源:互联网 发布:铁塔运维软件 编辑:程序博客网 时间:2024/05/16 18:18

sort

Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22164    Accepted Submission(s): 6650


题意为:给你n个整数,请按从大到小的顺序输出其中前m大的数。
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

直接排序由于数据过大会超时。

同时又因为数据不是“很大”而可以选用hash表来做

#include <iostream>using namespace std;#define MAX 1000000const int flag=500000;int Hash[MAX+5];int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){memset(Hash,0,sizeof(Hash));int t;int max=-MAX;for(int i=1;i<=n;i++){scanf("%d",&t);Hash[t+flag]=1;if(t+flag>max)max=t+flag;}int count=0;for(int i=max;i>=0;i--){if(Hash[i]){printf("%d",i-flag);count++;if(count==m){printf("\n");break;}elseprintf(" ");}}}}