您的位置:首页 > 其它

hdu 1425 Sort

2016-03-20 20:57 260 查看

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


Hint

请用VC/VC++提交


Author

LL


Source

ACM暑期集训队练习赛(三)


hash问题

范围为[-500000,500000],最大值为1000000,可开一个大数组。输入时将值对应的下标进行++操作

放入对应的位置即可

用快排也可水过。

#include<stdio.h>
#include<string.h>
int a[1000010];

int main()
{
int n,m;
int num;
while(scanf("%d%d",&n,&m)!=EOF){
memset(a,0,sizeof(a));
for(int i=0;i<n;i++){
scanf("%d",&num);
a[num+500000]++;
}
int count=0;
for(int i=1000000;i>=0;i--){
while(a[i]){
a[i]--;
count++;
printf("%d",i-500000);
if(count!=m)    printf(" ");
else    printf("\n");
}
if(count==m)    break;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: