hdu1280 前m大的数

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

 

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

 

 

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

 

Sample Input
  
  
4 4 1 2 3 4 4 5 5 3 6 4
 

 

Sample Output
  
  
7 6 5 5 11 10 9 9 8
 
 
 
我的代码是用STL写的,没有自己的任何算法。
 #include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int main(void)
{
 int n, m;
 vector<int >int_vec;
 priority_queue<int >int_pri;
 while(cin >> n >> m)
 {
  int_vec.clear();
  
  int temp = 0, i, j;
  for(i = 0; i< n ;i++)
  {
   cin >> temp;
   int_vec.push_back(temp);
   for(j = 0;j < i; j++)
   {
    //int_pri.push(int_vec[i] + int_vec[j]);
    int_pri.push(temp + int_vec[j]);
   }
  }
  for(i = 0; i < m-1; i++)
  {
   cout << int_pri.top() << " ";
   int_pri.pop();
  }
  cout << int_pri.top();
  cout << endl;
 }
 return 0;
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值