A higher resolution is required to access the IDE
Learning Opportunities
This puzzle can be solved using the following concepts. Practice using these concepts and improve your skills.
Statement
Goal
A grocery store has n lines of available shopping carts next to each other. Each line is indexed from 1 to n. Line i has carts_i shopping carts. To balance out the lines, there are specific rules regarding how customers must take and return shopping carts. Shopping carts are taken and returned one at a time by customers.Taking a shopping cart requires taking it from the line with the largest number of shopping carts. If there are multiple such lines, it must be taken from the line with the smallest index.
Returning a shopping cart requires returning it to the shortest line of shopping carts. If there are multiple shortest lines, return the shopping cart to the one with the largest index.
Given n, the initial number of shopping carts carts_i in each shopping cart line and a string actions containing a sequence of action characters (
Because the simulation can start from any initial condition, it is allowed to return carts that haven't been taken before.
Input
Line 1: The number of shopping cart lines n
Line 2: The initial number of shopping carts carts_i in each shopping cart line from 1 to n, separated by spaces
Line 3: The string actions, containing the sequence ofT (take) and R (return) actions
Line 2: The initial number of shopping carts carts_i in each shopping cart line from 1 to n, separated by spaces
Line 3: The string actions, containing the sequence of
Output
Line 1: The number of shopping carts in each line from 1 to n after executing all actions, separated by spaces, or IMPOSSIBLE if at any time, no carts are available and one shall be taken.
Constraints
0 < n <= 100
0 <= carts_i <= 250
0 < length(actions) <= 1000
0 <= carts_i <= 250
0 < length(actions) <= 1000
Example
Input
3 1 2 3 RTR
Output
2 2 3
A higher resolution is required to access the IDE