#P4940. Portal XM Energy Calculation

    ID: 18181 Type: Default 1000ms 256MiB

Portal XM Energy Calculation

Portal XM Energy Calculation

This problem involves simulating operations on two stacks (stack 0 and stack 1) as used by the Portal to compute XM Energy. You are given a sequence of commands. Every command (except for END) must output either SUCCESS (if the command is executed successfully) or UNSUCCESS (if the command fails to execute due to empty stack when trying to remove or retrieve an element). The commands are described below. All numeric values and operations in the description use \(\LaTeX\) format for formulas.

  • PUSH X NUM: Push the number NUM onto the top of stack \(X\) (where \(X\) is either 0 or 1).
  • POP X: Remove the top element from stack \(X\). If the stack is empty, output UNSUCCESS and do not modify any stack.
  • ADD X: Remove the top elements from both stacks \(0\) and \(1\) and compute their sum. Then, push the result into stack \(X\). If either stack is empty, output UNSUCCESS and do not modify any stack.
  • SUB X: Remove the top elements from both stacks \(0\) and \(1\) and compute the absolute difference \( |a - b| \). Then, push the result into stack \(X\). If either stack is empty, output UNSUCCESS and do not modify any stack.
  • DEL X: Clear all elements from stack \(X\) (regardless of whether it is empty). Always outputs SUCCESS.
  • MOVE X Y: While stack \(Y\) is not empty, remove the top element from stack \(Y\) and push it onto stack \(X\). Data guarantees that \(X \neq Y\). Always outputs SUCCESS.
  • SWAP: Exchange all the elements between stack 0 and stack 1. Always outputs SUCCESS.
  • END: This command terminates the sequence. On encountering END, output the contents of stack 0 and stack 1 in two separate lines, from top to bottom. If a stack is empty, output NONE for that stack. Then, also output SUCCESS.

Note: For each command, if the required operation of retrieving or deleting an element is not possible (due to an empty stack), output UNSUCCESS and ignore that command (i.e. do not modify any stacks). Otherwise, perform the command and output SUCCESS.

inputFormat

The input consists of multiple lines. Each line contains a command. The commands will be one of the following:

PUSH X NUM
POP X
ADD X
SUB X
DEL X
MOVE X Y
SWAP
END

The input is guaranteed to end with a single END command.

outputFormat

For each command in the input, output either SUCCESS if the command is executed successfully, or UNSUCCESS if the command failed due to a lack of elements (when trying to remove or retrieve an element). For the END command, first output two lines representing the contents of stack 0 and stack 1 respectively (each from top to bottom, or NONE if the stack is empty), and then output SUCCESS.

sample

PUSH 0 5
PUSH 1 3
ADD 0
POP 1
END
SUCCESS

SUCCESS SUCCESS UNSUCCESS 8 NONE SUCCESS

</p>