思路:a,b都是有序的,所以可以用小顶堆来存取数,先对a[0]+b[i]放进优先队列里,然后每次取出数时,查看取出数是由a几和b[i]相加的,然后将b的下一个元素加进去。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int n, a[N], b[N], t[N];
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >q;
int main() {
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
cin >> b[i], q.push(make_pair(a[0] + b[i], i)), t[i]++;
while(n--) {
cout << q.top().first << " ";
int i = q.top().second;
q.pop();
q.push(make_pair(a[t[i]++] + b[i], i));
}
return 0;
}