一名苦逼的OIer,想成为ACMer

Iowa_Battleship

洛谷1631 序列合并

原题链接

\(a[1] + b[1 \to n]\)扔到小根堆里,然后每次取堆顶并输出,再将堆顶的下一个和\(a[2] + b[x]\)扔入堆,这样依次操作下去即可。

#include<cstdio>
#include<queue>
using namespace std;
const int N = 1e5 + 10;
struct dd {
	int x, id, nx;
	bool operator < (const dd &b) const { return x > b.x; }
};
int a[N], b[N];
priority_queue<dd>q;
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
int main()
{
	int i, n, x, y;
	n = re();
	for (i = 1; i <= n; i++)
		a[i] = re();
	for (i = 1; i <= n; i++)
		b[i] = re();
	for (i = 1; i <= n; i++)
		q.push((dd){a[1] + b[i], i, 1});
	for (i = 1; i <= n; i++)
	{
		printf("%d ", q.top().x);
		x = q.top().id;
		y = q.top().nx + 1;
		q.pop();
		q.push((dd){a[y] + b[x], x, y});
	}
	return 0;
}

posted on 2018-11-06 10:04  Iowa_Battleship  阅读(114)  评论(0编辑  收藏  举报

导航