题目链接
求出a[ i ] + b[ 0 ] (0 <= i < n) 的值 ,存入优先队列中,每次取出当前最小值a[ i ] + b[ j ] ,且最小值出队,a[ i ] + b[ j + 1] 入队,最后得到的 res 队列中含有前 n 个最小值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int MAX_N = 1e5 + 10;
const int MOD = 1e9 + 7;
const double esp = 1e-9;
int n, m;
int a[MAX_N], b[MAX_N];
void solve(){
priority_queue<P, vector<P>, greater<P> > que;
priority_queue<int, vector<int>, greater<int> > res;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++)
scanf("%d", &b[i]);
for(int i = 0; i < n; i++){
que.push(P(a[i] + b[0], 0) );
res.push(a[i] + b[0]);
}
for(int i = 0; i < n; i++){
P p = que.top(); que.pop();
int t = p.first, v = p.second;
que.push(P(t - b[v] + b[v + 1], v + 1) );
res.push(t - b[v] + b[v + 1]);
}
printf("%d", res.top()); res.pop();
for(int i = 0; i < n - 1; i++) {printf(" %d", res.top()); res.pop(); }
}
int main(){
int TCASE = 1;
// scanf("%d", &TCASE);
while(TCASE--) solve();
return 0;
}