哈希思想最简单应用---hdoj1425

/*********************************************************
Copyright: G
Author:G
Date:2013-08-05 15:23:16
Destription:
哈希思想简单应用,通过空间换取时间
**********************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

#define max 500000

int hash[max<<1];//空间换取时间

int main()
{
    int n,m;
    int x;
    while(~scanf("%d %d",&n,&m))
    {
        while(n--)
        {
            scanf("%d",&x);
            hash[x+max]++;
        }

        for(int i = max<<1; m!=0; i--)
        {
            if(hash[i] && m > 1)
            {
                m--;
                printf("%d ",i-max);
                continue;
            }
            if(hash[i] && m == 1)
            {
                printf("%d\n",i-max);
                break;
            }
        }
    }
}


你可能感兴趣的:(哈希)