原题链接
将(a[1] + b[1 o 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;
}