当前位置: 代码迷 >> 综合 >> HDU-1280(哈希表求法)
  详细解决方案

HDU-1280(哈希表求法)

热度:9   发布时间:2023-11-23 13:00:07.0
题目名称:前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;}
}