eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Parencedence!

Parencedence!

Parencedence is a brand new two-player game that is sweeping the country (that country happens to be Liechtenstein, but no matter). The game is played as follows: a computer produces an arithmetic expression made up of integer values and the binary operators '\textbf{+}', '\textbf{-}' and '\textbf{*}'. There are no paren-theses in the expression. If Player \textbf{1} goes rst he/she can put parentheses around any one operator and its two operands; the parenthesized expression is evaluated and its value is used in its place. Player \textbf{2} then does the same, and the game proceeds accordingly, Player \textbf{1} and Player\textbf{2} alternating turns. Player \textbf{1}'s object is to maximize the nal value, while Player \textbf{2}'s object is to minimize it. A sample round might go as follows: A game of Parencedence is played in two rounds, each using the same initial unparenthesized expression: in the first round, Player \textbf{1} goes first, and in the second, Player \textbf{2} goes first (Player \textbf{1} is always trying to maximize the result and Player \textbf{2} is always trying to minimize the result in both rounds, regardless of who goes first). Let \textbf{r_1} be the result of the first round and \textbf{r_2} the result of the second round. If \textbf{r_1} > \textbf{-r_2}, then Player \textbf{1} wins; if \textbf{r_1} < \textbf{-r_2} then Player \textbf{2} wins; otherwise the game ends in a tie. Your job is to write a program to determine the nal result assuming both players play as well as possible. \InputFile The first line of the input le will contain an integer \textbf{n} indicating the number of test cases. The test cases will follow, one per line, each consisting of a positive integer \textbf{m} ≤ \textbf{9} followed by an arithmetic expression. The value of \textbf{m} indicates the number of binary operators in the arithmetic expression. The only operators used will be '\textbf{+}', '\textbf{-}' and '\textbf{*}'. The '\textbf{-}' operator can appear as both a unary and a binary operator. All binary operators will be surrounded by a single space on each side. There will be no space after any unary '\textbf{-}'. No combination of parentheses will ever result in an integer overflow or underflow. \OutputFile For each test case, output the case number followed by three lines. The first contains the first set of operands and operator to be parenthesized in round \textbf{1} (when Player \textbf{1} goes first) and \textbf{r_1}. The second line contains the analagous output for round \textbf{2}. The third line contains either the phrase "\textbf{Player 1 wins}", "\textbf{Player 2 wins}" or "\textbf{Tie}" depending on the values of \textbf{r_1} and \textbf{r_2}. In the first two output lines if there is a choice between which operator should be parenthesized first, use the one which comes earliest in the original expression. Follow the format used in the examples.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
2
4 3 - 6 * 4 - 7 + 12
2 45 - -67 - 3
Вихідні дані #1
Case 1:
Player 1 (7+12) leads to -2
Player 2 (3-6) leads to -27
Player 2 wins
Case 2:
Player 1 (-67-3) leads to 115
Player 2 (45--67) leads to 109
Player 1 wins
Джерело ACM ICPC East Central Regional Contest 2012 (ECNA 2012)