False Coin
InterviewTime limit1sMemory limit128 MB
Given recorded balance weighings of N coins with exactly one false coin, output the coin number if the results identify it uniquely, otherwise 0.
- Level
Medium5 of 10
- Topics
- Brute force, Implementation, Simulation, Array
- Solved
- No attempts yet
Problem
The “Gold Bar” bank has learned from reliable sources that its latest batch of coins contains exactly one false coin. The false coin differs in weight from the genuine ones (all genuine coins weigh the same); it may be either heavier or lighter.
To find the false coin, the bank clerks have only a simple two-pan balance. Placing coins on it tells you whether the total weight in the left pan is less than, greater than, or equal to the total weight in the right pan.
The clerks numbered the coins with distinct integers from to . They then performed several weighings, each time placing an equal number of coins in the left and right pans, and recorded the coin numbers used and the result of every weighing.
Write a program that determines the number of the false coin from these recorded results.
Input
The first line contains two integers and separated by a space, where is the number of coins () and is the number of weighings ().
The next lines describe the weighings, two lines per weighing.
- The first line of a weighing starts with an integer (), the number of coins placed in each pan. It is followed by the coin numbers in the left pan and then the coin numbers in the right pan. All numbers are separated by spaces.
- The second line is one of the characters
<,>, or=, giving the result of the weighing:<means the left pan is lighter than the right pan,>means the left pan is heavier than the right pan,=means the two pans weigh the same.
Output
If the given results determine the false coin uniquely, print its number. Otherwise print .