//题目链接
//http://acm.nyist.net/JudgeOnline/problem.php?pid=634
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 int a[100001], b[100001]; 7 struct node{ 8 int d; int b; 9 node(){} 10 node(int a, int b):d(a),b(b){} 11 bool operator < (const node& tmp) const{ 12 return d < tmp.d; //使队列维护从大到小的性质 13 } 14 }; 15 bool cmp(int ta, int tb){ 16 return ta > tb; 17 } 18 void merge(int *A, int *B, int *C, int n){ 19 priority_queue<node> pq; 20 int i; 21 for(i = 0; i < n; ++i) 22 pq.push(node(A[i] + B[0], 0)); //先入队列n个数(第一个是最大的) 23 for(i = 0; i < n; ++i){ 24 node tmp = pq.top(); pq.pop(); //每次最大的数出队列,并保存到数组C中 25 C[i] = tmp.d; 26 int b = tmp.b; 27 if(b + 1 < n) 28 pq.push(node(tmp.d - B[b] + B[b+1], b+1)); 29 //tmp.d = A[x] + B[b]; 30 //tmp.d - B[b] + B[b+1] 是小于等于tmp.d的一个数入队 31 } 32 } 33 int main() 34 { 35 int N, k, i; 36 while(scanf("%d%d",&N, &k) != EOF){ 37 for(i = 0; i < N; ++i) scanf("%d", &a[i]); 38 for(i = 0; i < N; ++i) scanf("%d", &b[i]); 39 sort(a, a+N, cmp); //从大到小排序 40 sort(b, b+N, cmp); 41 merge(a, b, a, k); 42 printf("%d", a[k-1]); 43 for(i = k-2; i >= 0; --i) 44 printf(" %d", a[i]); 45 puts(""); 46 } 47 return 0; 48 }