Amina has six coins, numbered from
A traditional balance scale has two pans. To use such a scale, you place a coin into each pan and the scale will determine which coin is heavier.
Amina's new scale is more complex. It has four pans, labeled
The four settings will instruct the scale to answer the following four questions:
- Which of the coins in pans
, , and is the heaviest? - Which of the coins in pans
, , and is the lightest? - Which of the coins in pans
, , and is the median? (This is the coin that is neither the heaviest nor the lightest of the three.) - Among the coins in pans
, , and , consider only the coins that are heavier than the coin on pan . If there are any such coins, which of these coins is the lightest? Otherwise, if there are no such coins, which of the coins in pans , , and is the lightest?
Task
Write a program that will order Amina's six coins according to their weight. The program can query Amina's scale to compare weights of coins. Your program will be given several test cases to solve, each corresponding to a new set of six coins.
Your program should implement the functions init
and orderCoins
. During each run of your program, the grader will first call init
exactly once. This gives you the number of test cases and allows you to initialize any variables. The grader will then call orderCoins()
once per test case.
void init(int T)
T
: The number of test cases your program will have to solve during this run, such that .
void orderCoins(void)
- This function is called exactly once per test case.
- The function should determine the correct order of Amina's coins by calling the grader functions
getHeaviest()
,getLightest()
,getMedian()
, and/orgetNextLightest()
. - Once the function knows the correct order, it should report it by calling the grader function
answer()
. - After calling
answer()
, the functionorderCoins()
should return.
Grader Functions
void answer(int W[6])
W
: An array of length containing the correct order of coins.W[0]
throughW[5]
should be the coin numbers (i.e., numbers from to ) in order from the lightest to the heaviest coin.- Your program should only call this function from
orderCoins()
, once per test case.
int getHeaviest(int A, int B, int C)
int getLightest(int A, int B, int C)
int getMedian(int A, int B, int C)
- These correspond to settings 1, 2 and 3 respectively for Amina's scale.
A
,B
,C
: The coins that are put in pans , , and , respectively. , , and should be three distinct integers, each between and inclusive.- Each function returns one of the numbers
A
,B
, andC
: the number of the appropriate coin. For example,getHeaviest(A, B, C)
returns the number of the heaviest of the three given coins.
int getNextLightest(int A, int B, int C, int D)
- This corresponds to setting 4 for Amina's scale.
A
,B
,C
,D
: The coins that are put in pans , , , and , respectively.A
,B
,C
, andD
should be four distinct integers, each between and inclusive.- The function returns one of the numbers
A
,B
, andC
: the number of the coin selected by the scale as described above for setting 4. That is, the returned coin is the lightest amongst those coins on pans , , and that are heavier than the coin in pan ; or, if none of them is heavier than the coin on pan , the returned coin is simply the lightest of all three coins on pans , , and .
Scoring
There are no subtasks in this problem. Instead, your score will be based on how many weighings (total number of calls to grader functions getLightest()
, getHeaviest()
, getMedian()
and/or getNextLightest()
) your program makes.
Your program will be run multiple times with multiple test cases in each run. Let
Let
Suppose the largest number of weighings amongst all test cases of all runs is
In particular, if your program makes at most
Example
Suppose the coins are ordered
Function call | Returns | Explanation |
---|---|---|
getMedian(4, 5, 6) |
6 | Coin |
getHeaviest(3, 1, 2) |
1 | Coin |
getNextLightest(2, 3, 4, 5) |
3 | Coins |
getNextLightest(1, 6, 3, 4) |
6 | Coins |
getHeaviest(3, 5, 6) |
5 | Coin |
getMedian(1, 5, 6) |
1 | Coin |
getMedian(2, 4, 6) |
6 | Coin |
answer({3, 4, 6, 2, 1, 5}) |
The program found the right answer for this test case. |
Comments