#P11067. Box Manipulation Challenge
Box Manipulation Challenge
Box Manipulation Challenge
This is a submit‐answer problem. You are given several boxes that initially contain some balls of different colors. There are 10 boxes in total. Initially, the first n boxes (n is specified by each subtask) may contain some balls, while the remaining boxes are empty. Each ball is represented by a lowercase letter. Initially there are at most three colors of balls, represented by a, b, and c, although in your program you may use any lowercase letters for the corresponding colors.
A box is said to contain a string if for every letter the count in the box is not less than the count in the string. A command to delete a string from a box means that for every letter in the string, one ball of that color is removed from the box (this can only be executed if the box contains the string). Conversely, adding (or "putting in") a string means that for every letter in the string, one ball of that color is added to the box.
Your program may use the following command:
change x s y t
Here, x and y are non‐negative integers no larger than 10, and s and t are non‐empty strings composed of lowercase letters or a single character '@' (if '@' then the string is interpreted as an empty string). If x is 0, then for k = 1 to 10, otherwise k = x. For each such box k, if it contains string s then delete s from it and put string t into box y (if y is 0, then put t into the same box k). In either case, the command is considered successfully executed, and on success the program immediately goes back to the previous breakpoint. If no box can successfully execute the command, the program proceeds to the next statement.
A line containing only #
denotes a breakpoint. Note that there is an implicit breakpoint at line 0 (which does not count) and the final line’s breakpoint is counted. The total number of statements is the count of both change
commands and breakpoints (excluding the virtual breakpoint at line 0). Also note the restrictions on the number of commands, boxes, ball counts, and string lengths (detailed in the problem statement).
You are required to complete the following 10 independent tasks. (The subtasks are ordered by a rule independent of difficulty.)
- Task 1 (n = 10, sum ≤ 100): Move all a balls into box 1, all b balls into box 2, and all c balls into box 3.
- Task 2 (n = 10, sum ≤ 100): Change all a balls to b balls, all b balls to c balls, and all c balls to a balls; these three operations should be performed simultaneously.
- Task 3 (n = 10, sum ≤ 100): For every box that does not contain any a balls, clear the box.
- Task 4 (n = 10, sum ≤ 100): For every non‐empty box that does not contain any a balls, add one a ball into it.
- Task 5 (n = 5, sum ≤ 5): Erase all initial balls and then place one ball per box such that all balls are arranged in increasing order by color (a < b < c) into boxes 1 to sum, and each box gets exactly one ball.
- Task 6 (n = 10, sum ≤ 100): In each box, keep only the ball of the color that appears most frequently. If there is a tie (i.e. multiple colors share the maximum frequency), clear the box (make it empty).
- Task 7 (n = 10, sum ≤ 100): In each box, keep only the balls of the most frequent color. In the event of a tie, keep the color with the smallest letter (a < b < c).
- Task 8 (n = 1, mx ≤ 10): Denote by A, B, and C the counts of a, b, and c balls respectively in the sole box. For every integer x satisfying 1 ≤ x ≤ 5, the final state of box x must contain exactly A + B·x + C·x2 balls of color a, and no balls of any other color.
- Task 9 (n = 5, max ≤ 10): Initially, all boxes have an equal total number of balls. For each box, sort the balls in increasing order and form a string. These strings are guaranteed to be pairwise distinct. For each such string, compute its lexicographical rank among the others and then change the box to contain a single letter corresponding to that rank (with the ordering a, b, c, d, e for ranks 1 to 5 respectively).
- Task 10 (n = 5, max ≤ 10): For the given 5 boxes, compute the prefix sum. That is, for each box, copy all balls from the boxes that come before it into itself.
Input Format: The input begins with parameters that guarantee the imposed bounds in each subtask. The details of the input format vary by subtask. In all cases, the first n boxes (n is given) describe the initial configuration, and the remaining boxes retain their original state.
Output Format: Your program should output a sequence of commands (each on a separate line) using the change
command and breakpoints (#
) as described. The program must end with a line containing a single #
.
Note: You must not exceed 100 statements in your program, and the actual execution must not traverse more than 4 × 105 statements, among other limits. The use of more than 10 boxes, or ball colors outside lowercase letters, or any ball count per color exceeding 108, is forbidden.
Subtasks are independent, and your solution must correctly transform the boxes according to the specific task provided.
inputFormat
The input format depends on the subtask. In general, the first line contains an integer representing the task number (from 1 to 10). The next line contains an integer n representing the number of boxes that initially may have balls. This is followed by n lines, each representing the string of balls (each character represents one ball). The remaining boxes (if any) are initially empty.
outputFormat
Your output should be a sequence of commands. Each command is either a change x s y t
command or a breakpoint, signified by a line containing only #
. The program must finish with a breakpoint on a single line. Your commands must transform the boxes to satisfy the conditions specified in the chosen task.
Note: The sample test cases below assume that a valid sequence of commands is produced. For the purpose of the sample solutions provided here, a trivial solution that outputs only a final breakpoint is given.
sample
1
10
aaa
bbb
ccc
#
</p>