Problem B: Sequence merging

Description

There are two sequences A and B of length N. Add a number in A and B to get N2 sum. Find the smallest N of these N2 sums.

Input

A positive integer N (1 <= N <= 100000) in the first line.
N integers Ai in the second row, satisfying Ai <= Ai + 1 and Ai <= 109
Third line N individual integers Bi, added Bi <= Bi + 1 and Bi <= 109

Output

The output is only one line, containing N integers, and the N smallest sums are output from small to large, with adjacent numbers separated by spaces.

Sample Input

3
2 6 6
1 4 8

Sample Output

3 6 7

HINT

It is recommended to use the minimum heap.

 

Reference link: The smallest N sum (priority queue)

The final AC code is as follows:

#include <bits / stdc ++. h>
 using  namespace std;
 const  int MAX = 1e5 + 2 ;
 struct Node {
     int index, sum;
     bool  operator <( const Node & t) const {
         return sum> t.sum; // Priority queue representation According to the size of sum 
    }
};
int main(){
    int i, n, a[MAX], b[MAX];
    Node temp, now;
    priority_queue<Node> q;
    while(cin >> n){
        for(i=0; i<n; i++) scanf("%d", &a[i]);
        for(i=0; i<n; i++){
            scanf("%d", &b[i]);
            temp.index = 0;
            temp.sum = b [i] + a [temp.index];   // index represents the number in a [] 
            q.push (temp);
        }
        for(i=0; i<n; i++){
            now = q.top();
            q.pop();
            if(now.index+1 < n){
                temp.index = now.index + 1;
                temp.sum = now.sum - a[now.index] + a[temp.index];
                q.push(temp);
            }
            printf("%d%c", now.sum, i==n-1?'\n':' ');
        }
    }  
    return 0;
}

Summary: At the beginning, I saw the prompt and used the minimum heap, but I did not expect a specific idea. I think the difficulty of this question is that although the two sets of data are monotonous and unreduced, the requirements and the minimum (taken from the two sequences respectively Number, each number can be reused) the first N numbers, so there is a lot of uncertainty. If the amount of data is not very large, it may be violent, but this question obviously does not work.

After referring to other people's ideas, I found that priority queues can be used (usually priority queues are used less, I really did not think of this idea). My own understanding of this idea is roughly as follows:

First, take the smallest element in the a [] array, which is a [0], add b [i] (0 <= i <n) respectively, and put it in the priority queue. Then it can be determined that the element of the head of the priority queue at this time, that is, the value of a [0] + b [0] is the smallest, which can be output, and a [1] + b [0] is entered into the priority queue, and so on Until n numbers are output.

The above process, the logic seems simple, but pay attention to a few points:

1. The elements stored in the priority queue are structure variables. To define the purpose of the structure, the sum variable in the structure stores the sum of two numbers, and the index records the subscript of a [], so as to update the ambush pen for each subsequent update.

2. Since the elements stored in the priority queue are structure variables, it is necessary to customize the container for comparison. However, this definition method is really not easy to understand, let me remember for the time being.

3. The judgment of the index range before entering the team can be omitted in this question (try it, it has no effect).

Guess you like

Origin www.cnblogs.com/heyour/p/12711821.html