2015
04-16

# Working at the Restaurant

Last night, Tom went on a date with a really nice girl. However, he forgot to take his credit card with him and he had no cash in his wallet, so he ended up working at the restaurant to pay for the bill. His task is to take plates from the waiter when he comes from the tables, and pass them along when the diswasher requests them. It is very important for the plates to be washed in the same order as they are brought from the tables, as otherwise it could take too long before a plate is washed, and leftover food might get stuck. Trying to hold all the plates in his hands is probably not a great idea, so Tom puts them on a table as soon as the waiter hands them over to him, and picks them up from the table again when the time comes to pass them along to the dishwasher. There is space for only two piles of plates on the table, which will be referred to as pile 1 and pile 2. There is only one table Tom can use. Tom won last year’s SWERC, so he is certainly capable of optimizing for efficiency. You have to output a transcript of one possible way in which Tom might decide to organize the plates on the table during the process, given the sequence of plates and requests he receives.

The input has several test cases. Each case begins with a line containing a number N (1 <=N <= 1 000), followed by N lines, which contain either DROP m or TAKE m, where m > 0 is the number of plates to take or drop. DROP m represents that the next event is the waiter bringing m plates to Tom, so he has to drop them on the table, while TAKE m represents that the next event is Tom taking m plates from the table and passing them along in the right order. You can assume that he never receives a TAKE m instruction when there are fewer than m plates on the table, and that the sum M of all values of m corresponding to DROP operations does not exceed 100 000. Note that there might be plates left on Tom’s table when the last request is issued, as Tom might be relieved of his duty to stay until the restaurant closes. The input ends with a line with N = 0, which must not be processed.

The input has several test cases. Each case begins with a line containing a number N (1 <=N <= 1 000), followed by N lines, which contain either DROP m or TAKE m, where m > 0 is the number of plates to take or drop. DROP m represents that the next event is the waiter bringing m plates to Tom, so he has to drop them on the table, while TAKE m represents that the next event is Tom taking m plates from the table and passing them along in the right order. You can assume that he never receives a TAKE m instruction when there are fewer than m plates on the table, and that the sum M of all values of m corresponding to DROP operations does not exceed 100 000. Note that there might be plates left on Tom’s table when the last request is issued, as Tom might be relieved of his duty to stay until the restaurant closes. The input ends with a line with N = 0, which must not be processed.

3
DROP 100
TAKE 50
TAKE 20
3
DROP 3
DROP 5
TAKE 8
0

DROP 2 100
MOVE 2->1 100
TAKE 1 50
TAKE 1 20

DROP 2 3
DROP 2 5
MOVE 2->1 8
TAKE 1 8

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮