[Solution] Ball Sequence CodeChef Solution
Problem
There is an infinite line of people, with person numbered standing on the right of person numbered . Chef can do types of operations to this line of people:
- Type : Give a ball to the person number .
If there exits a person with two balls, they drop one ball and give the other ball to the person on their right, and this repeats until everyone has at most ball. - Type : Everyone gives their ball to the person on their left simultaneously. Since there is no one to the left of person , they would drop their original ball if they have one.
Chef gives a total of instructions, out of which instructions are of type .
Each instruction is numbered from to . The indices of instructions of type are given by the array . The rest operations are of type .
Find the number of balls that have been dropped by the end of all the instructions.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains two space-separated integers and — the total number of operations and number of operations of type .
- The next line contains the array space-separated integers - the indices of instructions of type .
Output Format
For each test case, output on a new line the number of balls that have been dropped by the end of all the instructions.
Explanation:
Test case : The operations are performed as follows:
- Type : Only person has a ball. Till now, balls dropped.
- Type : Person has balls. So he drops ball and passes to its right. Now, only person has a ball. Till now, ball dropped.
- Type : Person passes the ball to its left. Now, only person has a ball. Till now, ball dropped.
- Type : Person drops the ball. Till now, balls dropped.
- Type : Only person has a ball. Till now, balls dropped.
In total, balls are dropped.
Test case : Only one operation occurs which is of type , so no balls are dropped.
No comments:
Post a Comment