hdu 1280

来源:互联网 发布:校园小说改编的网络剧 编辑:程序博客网 时间:2024/06/18 08:00
F - 前m大的数
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
SubmitStatus Practice HDU 1280 uDebug

Description

Input

Output

Sample Input

Sample Output

Hint

Description

还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。
给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。

Input

输入可能包含多组数据,其中每组数据包括两行:
第一行两个数N和M,
第二行N个数,表示该序列。

Output

对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。

Sample Input

4 41 2 3 44 55 3 6 4

Sample Output

7 6 5 511 10 9 9 8
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=3000;
int sum[3005];
int sum1[3000*3000/2];
int n,m;
int cmp(const int a ,const int b)
{
    return a>b;
}
int main()
{
    while(cin>>n>>m)
    {
        int k=0;
        for(int i=0; i<n; i++)
        {
            cin>>sum[i];
        }
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                sum1[k++]=sum[i]+sum[j];
            }
        }
        sort(sum1,sum1+k,cmp);
        for(int i=0; i<m; i++)
        {
            if(i==0)
            {
                cout<<sum1[i];
            }
            else
            {
                cout<<" "<<sum1[i];
            }
        }
        cout<<endl;
    }
    return 0;
}
0 0
原创粉丝点击