HDU-1280(哈希表求法)

来源:互联网 发布:北京网络咨询医生招聘 编辑:程序博客网 时间:2024/06/06 19:29
题目名称:前m大的数  题目链接:https://vjudge.net/problem/HDU-1280
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define getHassVal(i, j) arr[i] + arr[j]using namespace std;const int MAXN = 1500000;int arr[5000];int hashMap[MAXN];int main(){    int n, m;    while(scanf("%d%d", &n, &m) != EOF)    {        memset(hashMap, 0, sizeof(hashMap));        for(int i = 0; i < n; ++i) cin >> arr[i];        for(int i = 0; i < n; ++i)            for(int j = i + 1; j < n; ++j) hashMap[getHassVal(i, j)]++;        int flag = 0;        for(int i = MAXN - 1; i > 0&&m>0; )        {            if(hashMap[i] == 0){i--; continue;}            if(flag) cout << ' ' << i;            else cout << i;            flag = 1;            hashMap[i]--;            m--;        }        cout << endl;    }}
原创粉丝点击