HDU-1280 Front m biggest Numbers

See the article on https://dyingdown.github.io/2019/12/16/HDU-1280%20Front-m-biggest-Numbers/

Front m biggest Numbers

Remember the assignment that Gardon assigned to Xiaoxi? (1005 of the last match) Actually Xiaoxi has retrieved the original number sheet. Now she wants to confirm whether her answer is correct, but the entire answer is a huge table. Xiaoxi just wants you to put the answer Tell her the largest number of M’s.
Given a sequence containing N (N <= 3000) positive integers, each of which does not exceed 5000, add N * (N-1) / 2 sums obtained by adding them one by one, and find out where the first M is large (M <= 1000) and in ascending order.

Input

The input may contain multiple sets of data, each of which consists of two rows:
The first two numbers N and M,
The number N in the second line indicates the sequence.

Output

For each set of input data, M numbers are output, indicating the result. The output should be in order from largest to smallest.

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

Analysis

First sort all the original numbers. And then, add the first m biggest numbers one by one, we get biggest sums of first m * m numbers. Then sort. Finally out put the first m numbers.

Code

#include<bits/stdc++.h>

using namespace std;

int a[1000000];
int ans[2000000];
int main() {
	int n, m;
	while(cin >> n >> m) {
		for(int i = 0; i < n; i ++) {
			cin >> a[i];
		}
		sort(a, a + n, greater<int>());
		int k = 0;
		for(int i = 0; i < m; i ++) {
			for (int j = i + 1; j < m; j ++) {
				ans[k ++] = a[i] + a[j];
			}
		}
		sort(ans, ans + k, greater<int>());
		for(int i = 0; i < m; i ++) {
			if(i == m - 1)
				cout << ans[i] << endl;
			else cout << ans[i] << " ";
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值