Timus #1494. Monobilliards

题目:有n个数按照1,2,3…n的顺序入栈,在每一步入栈的过程中(严格按照入栈顺序,即从先到后),可以按顺序出栈(严格按照出栈顺序,即从后到先),如果可以得到题目所input的顺序,则输出not a proof,否则为cheater。

例子讲解:
input:
4
1 3 2 4
output:
not a proof

题目为1,2,3,4入栈顺序
(1). 1入栈,1出栈;
(2). 2,3入栈,3,2出栈;
(3). 4入栈,4出栈
存在这种可能,输出not a proof

input:
3
3 1 2
output:
cheater

题目为1,2,3入栈顺序
(1). 1,2,3入栈,3 出栈
因为接下来出栈必须为2,所以不存在这种可能,输出cheater

解析: 堆栈

Using the idea of stack. Input numbers (by order 1,2,3,…,n) to stack one by one, remove it out of the stack (by input number’s order) in each insertion. See if the stack is empty in the end.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

浙ICP备2021019730-1    浙公网安备 33010902002953号
Copyright © 2024 PanCake