POJ2442 Sequence

知识点:堆,数学归纳法

这个题是两个序列合并的升级版,首先我们要会两个序列合并,然后我们拿合并完的序列和第三个序列合并,得到新的最小值序列,这样一直合并下去,最后得到的就是最值的序列,

一个编程的细节就是我们输出答案的数组一开始也是要赋值的,因为输入的序列可能只有一个的情况

#include <bits/stdc++.h>

using namespace std;

const int N = 2005;

int a[N], b[N], c[N];

struct node {
	int x, y, flag;
	node() {}
	node(int a, int b, int c): x(a), y(b), flag(c) {}
	bool operator < (const node &t) const {
		return a[x] + b[y] > a[t.x] + b[t.y];
	}
};

int main() {
	int T;
	cin >> T;
	while (T--) {
		int m, n;
		cin >> m >> n;
		for (int i = 1; i <= n; i++) scanf("%d", a + i);
		sort(a + 1, a + n + 1);
		for (int i = 1; i <= n; i++) c[i] = a[i];
		while (--m) {
			for (int i = 1; i <= n; i++) scanf("%d", b + i);
			sort(b + 1, b + n + 1);
			priority_queue<node> q;
			q.push(node(2, 1, 0));
			q.push(node(1, 2, 1));
			c[1] = a[1] + b[1];
			for (int i = 2; i <= n; i++) {
				node now = q.top(); q.pop();
				c[i] = a[now.x] + b[now.y];
				q.push(node(now.x, now.y + 1, 1));
				if (!now.flag) q.push(node(now.x + 1, now.y, 0));
			}
			for (int i = 1; i <= n; i++) a[i] = c[i];
		}
		for (int i = 1; i <= n; i++) {
			printf("%d%s", c[i], i < n ? " " : "\n");
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值