【题解】LuoGu1631:序列合并

原题传送门

  • a 1 + b 1 < = a 1 + b 2 < = . . . < = a 1 + b n a_1+b_1<=a_1+b_2<=...<=a_1+b_n a1+b1<=a1+b2<=...<=a1+bn
  • a 2 + b 1 < = a 2 + b 2 < = . . . < = a 2 + b n a_2+b_1<=a_2+b_2<=...<=a_2+b_n a2+b1<=a2+b2<=...<=a2+bn
  • a n + b 1 < = a n + b 2 < = . . . < = a n + b n a_n+b_1<=a_n+b_2<=...<=a_n+b_n an+b1<=an+b2<=...<=an+bn

一开始先把 a i + b 1 a_i+b_1 ai+b1放进小根堆,每次取出最小的,比如 a i + b j a_i+b_j ai+bj为当前最小,就输出后把 a i + b j + 1 a_i+b_{j+1} ai+bj+1放进堆里

Code:

#include 
#define maxn 100010
using namespace std;
struct heap{
	int x, val;
	bool operator < (const heap &x) const {return x.val < val;}
};
priority_queue <heap> q;
int n, a[maxn], b[maxn], pos[maxn];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int main(){
	n = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	for (int i = 1; i <= n; ++i) b[i] = read();
	for (int i = 1; i <= n; ++i) q.push((heap){i, a[i] + b[pos[i] = 1]});
	for (int i = 1; i <= n; ++i){
		heap tmp = q.top(); q.pop();
		printf("%d ", tmp.val);
		q.push((heap){tmp.x, a[tmp.x] + b[++pos[tmp.x]]});
	}
	return 0;
}

你可能感兴趣的:(题解,LuoGu,优先队列(堆))