hdu1280 前m大的数(哈希打表)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Flynn_curry/article/details/61427895


http://acm.hdu.edu.cn/showproblem.php?pid=1280

题意:中文题


思路:3000*3000个数数量很大,所以只需对其大小哈希打表即可。水题秒之。


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <iostream>

using namespace std;

typedef long long ll;
const int N = 10000;
const int INF = 0x3f3f3f3f;

int table[N], num[3001];

int main()
{
  //  freopen("in.txt", "r", stdin);
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        memset(table, 0, sizeof(table));
        int Max = -INF;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &num[i]);
            Max = max(Max, num[i]);
            for(int j = 1; j < i; j++)
            {
                table[num[i]+num[j]]++;
            }
        }
        int flag = 0;
        for(int i = 2*Max; i >= 1; i--)
        {
            if(m==0) break;
            if(table[i]!=0)
            {
                if(flag) printf(" ");
                flag = 1;
                printf("%d", i);
                table[i]--;
                m--;
                i++;
            }
        }
     //   printf("%d", Max);
        printf("\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Flynn_curry/article/details/61427895