UVa 11111 Generalized Matrioshkas:栈

11111 - Generalized Matrioshkas

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2052

Vladimir worked for years making matrioshkas, those nesting dolls that certainly represent truly Russian craft. A matrioshka is a doll that may be opened in two halves, so that one finds another doll inside. Then this doll may be opened to find another one inside it. This can be repeated several times, till a final doll -that cannot be opened- is reached.

Recently, Vladimir realized that the idea of nesting dolls might be generalized to nesting toys. Indeed, he has designed toys that contain toys but in a more general sense. One of these toys may be opened in two halves and it may have more than one toy inside it. That is the new feature that Vladimir wants to introduce in his new line of toys.

Vladimir has developed a notation to describe how nesting toys should be constructed. A toy is represented with a positive integer, according to its size. More precisely: if when opening the toy represented bym we find the toys represented by n1, n2, ..., nr, it must be true thatn1 +n2 + ... +nr <m. And if this is the case, we say that toymcontains directly the toysn1,n2,...,nr . It should be clear that toys that may be contained in any of the toysn1,n2,...,nr are not considered as directly contained in the toym.

A generalized matrioshka is denoted with a non-empty sequence of non zero integers of the form:

a1    a2    ...    aN

such that toy k is represented in the sequence with two integers-k andk, with the negative one occurring in the sequence first that the positive one.

For example, the sequence

-9     -7     -2    2     -3     -2     -1    1    2    3    7    9

represents a generalized matrioshka conformed by six toys, namely, 1, 2 (twice), 3, 7 and 9. Note that toy 7 contains directly toys2 and3. Note that the first copy of toy2 occurs left from the second one and that the second copy contains directly a toy1. It would be wrong to understand that the first-2 and the last2 should be paired.

On the other hand, the following sequences do not describe generalized matrioshkas:

-9     -7     -2    2     -3     -1     -2    2    1    3    7    9

because toy 2 is bigger than toy 1 and cannot be allocated inside it.

 -9     -7     -2    2     -3     -2     -1    1    2    3    7     -2    2    9

because 7 and 2 may not be allocated together inside9.  

-9     -7     -2    2     -3     -1     -2    3    2    1    7    9

because there is a nesting problem within toy 3.

Your problem is to write a program to help Vladimir telling good designs from bad ones.

Input

The input file contains several test cases, each one of them in a separate line. Each test case is a sequence of non zero integers, each one with an absolute value less than107.

Output

Output texts for each input case are presented in the same order that input is read.

For each test case the answer must be a line of the form :-) Matrioshka!

if the design describes a generalized matrioshka. In other case, the answer should be of the form

:-( Try again.

Sample Input

-9 -7 -2 2 -3 -2 -1 1 2 3 7 9
-9 -7 -2 2 -3 -1 -2 2 1 3 7 9
-9 -7 -2 2 -3 -1 -2 3 2 1 7 9
-100 -50 -6 6 50 100
-100 -50 -6 6 45 100
-10 -5 -2 2 5 -4 -3 3 4 10
-9 -5 -2 2 5 -4 -3 3 4 9

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索cannot find -lgcc
, 11111
, opened
, be
that
generalized、generalized inverse、generalizedhough、generalized function、generalized mean,以便于您获取更多的相关知识。

时间: 2024-08-22 14:13:08

UVa 11111 Generalized Matrioshkas:栈的相关文章

uva 11111 - Generalized Matrioshkas

点击打开链接 题目意思:   这一题题目真心不好看懂,所以呢,我就大概描述一下,题目是说有一个娃娃大小为k可以拆成 -k 和 k ,然后娃娃里面可以嵌套物品,但是规定内层娃娃的大小是不能超过 外层的大小的, 现在给定一个娃娃的尺寸大小序列,左边的是负数,问我们这个序列是不是一个物品所拆开的,相应输出题目所要求的内容. 解题思路:   1:这一题和括号匹配非常像,只是多了一个判断内层的大小和外层的关系.我们可以建立一个Toy结构体,存储当前娃娃的大小以及当前剩余的空间,那么我们用一个栈来存储结构体

UVa 1111 Generalized Matrioshkas 数据结构专题

题目链接接: http://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=2052 题目类型: 数据结构, 链表 题目大意: 这题的题意比较难懂,看了好几变才明白.  就是有一个可以嵌套娃娃的娃娃,然后嵌套在里面的娃娃又可以继续嵌套娃娃. 然后要求直接嵌套在里面(内一层)的娃娃的尺寸大小之和不能超过外面的. 例如,-3 -

UVa 673 Parentheses Balance (栈)

673 - Parentheses Balance Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614 You are given a string consisting of parentheses () and []. A string of thi

UVa 10700:Camel trading

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 原题: Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the

UVa 10305:Ordering Tasks , 经典的拓扑排序

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1246 题目类型: 拓扑排序 题目: John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is onl

UVa 10054:The Necklace, 欧拉回路+打印路径

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=995 题目类型: 欧拉回路 题目: My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace sha

UVa 127:"Accordian" Patience 数据结构专题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=63 题目类型: 数据结构, 链表 题意: 发一副牌(52张,分两行), 从左往右开始, 对于当前这张牌,如果有左边第一个或左边第三个的数值(face-value)或者形状(suit)是一样的,就把该张牌移动到左边第一行或者第三行(如果左1和左3都可以移,则优先移动到左3),移动后,

UVa 101 The Blocks Problem 数据结构专题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=37 题目类型: 数据结构, 二叉树 题意: 有N个位置, 编号为 0-N-1, 初始下,各个位置上放置这和位置编号相同的砖块,即砖块1,砖块2--砖块N-1. 然后有四种命令操作方式: 1.move a onto b :把砖a移动到砖b上面,如果a

UVA 442:Matrix Chain Multiplication 数据结构专题

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=383 题目类型: 数据结构, 链表 样例输入: 9 A 50 10 B 10 20 C 20 5 D 30 35 E 35 15 F 15 5 G 5 10 H 10 20 I 20 25 A B C (AA) (AB) (AC) (A(BC)) (