Journey
InterviewTime limit2sMemory limit1024 MB
Given taken seats and the seats Lida wants (out of 54), find the fewest ticket ranges that buy every wanted seat without buying any unwanted free seat.
- Level
Medium4 of 10
- Topics
- Greedy, Implementation
- Solved
- No attempts yet
Statement
Lida was bored in the northern city of Saint Petersburg. She decided to travel with her friends, so she went to the railway station and found a ticket system there. One request books all free seats in the range from to (, inclusive), and it ignores seats that are already taken in that range. Lida is in a hurry, so she wants to make the smallest possible number of ticket requests. She knows which seat each of her friends wants, so she only buys tickets for those seats.
Given the list of taken seats and the seats Lida needs, find the minimum number of requests needed to buy them.
Input
The first line contains two integers (), the number of seats already taken, and (), the number of people traveling.
The second line contains distinct integers (), the taken seats.
The third line contains distinct integers (), the seats that Lida and her friends want.
Output
On the first line, print the number of requests . Then print lines, each with two numbers: the left and right bounds of one request.
If the tickets cannot be bought the way Lida and her friends want, print .