HDU 1425 Hash求解

本题用快排也可以过 (时间复杂度(nlgn)); Hash 时间复杂度 n;

快排:
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int maxn=1000000+10;
  5. int a[maxn];
  6. bool cmp(int a,int b){return a>b;}
  7. int main(){
  8.     int n,m;
  9.     while(scanf("%d%d",&n,&m)==2){
  10.         for(int i=0;i<n;i++)scanf("%d",&a[i]);
  11.         sort(a,a+n,cmp);
  12.         printf("%d",a[0]);
  13.         for(int i=1;i<m;i++)printf(" %d",a[i]);
  14.         printf("\n");
  15.     }
  16.     return 0;
  17. }


Hash:
  1. #include<cstdio>
  2. #include<cstring>
  3. const int maxn=1000000+10;
  4. int hash[maxn];
  5. int main(){
  6.     int n,m;
  7.     while(scanf("%d%d",&n,&m)==2){
  8.         memset(hash,0,sizeof(hash));
  9.         for(int i=0;i<n;i++){
  10.             int temp;
  11.         scanf("%d",&temp);
  12.         hash[500000+temp]=1; 
  13.         }
  14.         int t=m;
  15.         for(int i=maxn-1;m>0;i--){              //有一点不清楚的是这里不能写出 i=sizeof(hash)-1;????
  16.             if(hash[i]){
  17.                   if(m==t)
  18.                   printf("%d",i-500000);
  19.                   else printf(" %d",i-500000);
  20.                   m--;
  21.             }
  22.         }
  23.         printf("\n");
  24.     }
  25.     return 0;
  26. }

练习本题的目的是学会运用Hash求解问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值