http://acm.hdu.edu.cn/showproblem.php?pid=1425
时间很慢。。。
| Accepted | 1425 | 671MS | 4204K | 2394 B | G++ |
#include<iostream>
#include<vector>
#include<map>
#include<stack>
#include<algorithm>
#include<queue>
#include<list>
#include<set>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int hash[1000010];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
memset(hash,0,sizeof(hash));
for(i=0;i<n;i++)
{
int a;
scanf("%d",&a);
hash[a+500000]=1;
}
int cnt=0;
for(i=1000000;i>=0;i--)
{
if(hash[i]){
cnt++;
printf("%d",i-500000);
if(cnt!=m)printf(" ");
}
if(cnt==m)break;
}
printf("\n");
}
return 0;
}
本文提供了一种解决HDU 1425问题的有效算法实现方案,利用哈希表进行数据存储并快速查找。通过两次遍历输入数据,首先将所有整数映射到哈希表中,然后逆序遍历哈希表找到前m个出现的整数。
273

被折叠的 条评论
为什么被折叠?



