Dima and Containers (Codeforces 358C)
Tags: Implement, constructive algorithms

Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:  Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.  Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers. Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!

Input: The first line contains integer n (1 ≤ n ≤ 105) — the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:  Integer a (1 ≤ a ≤ 105) means that Inna gives Dima a command to add number a into one of containers.  Integer 0 shows that Inna asks Dima to make at most three extractions from different containers. 

Output: Each command of the input must correspond to one line of the output — Dima's action.For the command of the first type (adding) print one word that corresponds to Dima's choice:  pushStack — add to the end of the stack;  pushQueue — add to the end of the queue;  pushFront — add to the beginning of the deck;  pushBack — add to the end of the deck. For a command of the second type first print an integer k (0 ≤ k ≤ 3), that shows the number of extract operations, then print k words separated by space. The words can be:  popStack — extract from the end of the stack;  popQueue — extract from the beginning of the line;  popFront — extract from the beginning from the deck;  popBack — extract from the end of the deck. The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.


Sample Inputs:
10
0
1
0
1
2
0
1
2
3
0

4
1
2
3
0

Sample Outputs:
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront

pushStack
pushQueue
pushFront
3 popStack popQueue popFront

Solution (Python):
(i, n) = (0, int(input()))
s = ['pushQueue'] * n
(a, b, c) = (' popQueue', ' popStack', ' popBack')
p = ['0', '1' + a, '2' + a + b, '3' + a + b + c]
t = []
for j in range(n):
    x = int(input())
    if x:
        t.append((x, j))
        continue
    t = sorted((k for (x, k) in sorted(t)[-3:]))
    k = len(t)
    if k > 0:
        s[i:t[0]] = ['pushStack'] * (t[0] - i)
    if k > 1:
        s[t[1]] = 'pushStack'
    if k > 2:
        s[t[2]] = 'pushBack'
    (i, t, s[j]) = (j + 1, [], p[k])
print('\n'.join(s))

Solution (Python):
operations = []
nums = []
for _ in range(int(input())):
    i = int(input())
    if i == 0:
        top3 = sorted(nums)[-3:]
        stack_used = False
        queue_used = False
        deck_used = False
        for n in nums:
            if n in top3:
                if not stack_used:
                    stack_used = True
                    operations.append('pushStack')
                elif not queue_used:
                    queue_used = True
                    operations.append('pushQueue')
                elif not deck_used:
                    deck_used = True
                    operations.append('pushFront')
                top3.remove(n)
            else:
                operations.append('pushBack')
        nums = []
        operations.append(stack_used + queue_used + deck_used)
        if stack_used:
            operations.append('popStack')
        if queue_used:
            operations.append('popQueue')
        if deck_used:
            operations.append('popFront')
    else:
        nums.append(i)
for _ in range(len(nums)):
    operations.append('pushBack')
print('\n'.join((str(o) for o in operations)))

Solution (Python):
(i, n) = (0, int(input()))
s = ['pushQueue'] * n
(a, b, c) = (' popQueue', ' popStack', ' popBack')
p = ['0', '1' + a, '2' + a + b, '3' + a + b + c]
t = []
for j in range(n):
    x = int(input())
    if x:
        t.append((x, j))
        continue
    t = sorted((k for (x, k) in sorted(t)[-3:]))
    k = len(t)
    if k > 0:
        s[i:t[0]] = ['pushStack'] * (t[0] - i)
    if k > 1:
        s[t[1]] = 'pushStack'
    if k > 2:
        s[t[2]] = 'pushBack'
    (i, t, s[j]) = (j + 1, [], p[k])
print('\n'.join(s))