Mice
InterviewTime limit1sMemory limit1024 MB
Given mice and cheese positions on a line, find the movement direction and the fewest hungry mice under two eating rules.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Binary search
- Solved
- No attempts yet
Statement
mice are placed in a row. Some pieces of cheese are placed somewhere among them. All mice move in the same direction, either only to the left or only to the right, and no mouse overtakes another.

Each mouse has two options.
Option (A): Each mouse eats only one piece of cheese, the first uneaten piece it meets.
Option (B): Each mouse eats all uneaten pieces of cheese it meets along its path.
A position holds at most one mouse, but there can be more pieces of cheese. A position can contain a mouse and cheese at the same time.
Write a program mice that, for each option, prints the direction of movement and the minimum number of mice that eat no cheese.
Input
The first line contains an integer , the number of mice. The second line contains positive integers , the position of each mouse. The third line contains an integer , the number of pieces of cheese. The fourth line contains positive integers , the position of each piece of cheese.
Output
The first line prints the answer for option (A) as a symbol and a number , separated by one space. is the direction of movement, and is the minimum number of mice that stay hungry.
The second line prints the answer for option (B) in the same format, with and having the same meanings as and .
and are one of L, R, or D. L means left, R means right, and D means the minimum is the same whether the mice move left or right.
Limits