Merging survey records
Statement
Two survey teams went onto a construction site to check how far the ground had moved. Team A measured N points and team B measured M points.
Each measurement is an integer giving how much the elevation of that point changed from last year. A positive value means the ground rose by that much, and a negative value means it sank by that much.
After measuring, each team sorted its own records from the smallest value to the largest. The site manager wants to see the records of the two teams merged into one line, again from the smallest value to the largest.
Write a program that takes the records of the two teams, each already in ascending order, and prints the merged result in ascending order.
For example, suppose N=2 and M=2, and the records of the two teams are given by the table below.
| Team | First record | Second record |
|---|---|---|
| Team A | 3 | 5 |
| Team B | 2 | 9 |
Each record is already in ascending order. So there is no need to sort everything from scratch. It is enough to compare only the two front values that have not been taken yet, and to take the smaller one each time. The table below follows that step by step.
| Step | Value at the front of team A | Value at the front of team B | Value taken | Taken from |
|---|---|---|---|---|
| 1 | 3 | 2 | 2 | Team B |
| 2 | 3 | 9 | 3 | Team A |
| 3 | 5 | 9 | 5 | Team A |
| 4 | No value left | 9 | 9 | Team B |
Once one record runs out, append the values left on the other side in order. The merged record is 2 3 5 9.
When the two front values are equal, it makes no difference which side you take from first. The two values are the same, so the result looks the same either way.
The same value may appear several times inside one team's record, or across the records of the two teams. In that case print it as many times as it appears.
Input and Output Examples
Example 1
Input
2 2 3 5 2 9
Output
2 3 5 9
Example 2
Input
3 3 32251340 378089284 420241381 -341524412 302404239 826177827
Output
-341524412 32251340 302404239 378089284 420241381 826177827
Example 3
Input
2 1 -535832937 224336059 603724148
Output
-535832937 224336059 603724148
Input
The first line holds the number of points team A measured N and the number of points team B measured M, separated by a space. N and M are each a natural number from 1 to 1,000,000.
The second line holds the N records of team A, separated by spaces, all on one line.
The third line holds the M records of team B, separated by spaces, all on one line.
Each measurement is an integer whose absolute value is at most 1,000,000,000. The records of both teams are given in ascending order, and the same value may appear several times.
The whole input is at most about 8.8MB. One line can hold up to 1,000,000 numbers, so read the input line by line and split each line on whitespace.
Output
Print the N+M merged values in ascending order on the first line, separated by spaces. Put one space only between two values, and put no space after the last value on the line. There are up to 2,000,000 values to print, so do not print them one at a time. Join them into a single string and print that string in one go.
Requirements
- The records of both teams are already given in ascending order
- If a value appears several times, print it as many times as it appears
- A measurement can be negative
- The output is one line, and no space is left after the last value
- The merged record runs up to 2,000,000 values, so putting the two records end to end and sorting them again is tight on both time and memory
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 512MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
2 1 -535832937 224336059 603724148
-535832937 224336059 603724148
3 3 32251340 378089284 420241381 -341524412 302404239 826177827
-341524412 32251340 302404239 378089284 420241381 826177827
2 2 3 5 2 9
2 3 5 9
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.