Problem 3: Library Stack Organization Time limit: 1 seconds A librarian needs to…
- User
Problem 3: Library Stack Organization Time limit: 1 seconds A librarian needs to organize three stacks of books to ensure that each stack contains books of only one genre. The goal is to minimize the number of moves required to rearrange the stacks in this way. Each stack currently contains a mix of three genres: ● S: Science ● F: Fiction ● H: History You are given the initial count of each genre in each stack. For instance: ● S1, F1, H1 represent the number of Science, Fiction, and History books in Stack 1, respectively. ● S2, F2, H2 for Stack 2. ● S3, F3, H3 for Stack 3. The goal is to organize the stacks so that: ● One stack contains only Science books. ● Another stack contains only Fiction books. ● The remaining stack contains only History books. Your task is to find the arrangement that minimizes the total number of moves required to achieve this configuration, where moving one book from one stack to another counts as one move. Input First line contains the number of test cases (t) 1<t<=105 . For each test case a single line contains nine integers: S1 F1 H1 S2 F2 H2 S3 F3 H3. Each integer represents the number of books in the corresponding genre for each stack (0 ≤ books ≤ 100). Output The final sequence of the stack, followed by the number of minimum moves required to achieve that arrangement of the stack. If there are multiple solutions with minimum steps, output the lexicographically smallest one. Sample input & output The following is an example of a sample input and corresponding correct outputs. Sample input Sample Output 2 1 1 2 3 5 8 10 3 7 4 4 4 4 4 4 4 4 4 FHS 21 FHS 24
- Assistant