uva 11111 - Generalized Matrioshkas

  Problem B - Generalized Matrioshkas 

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 by m we find the toys represented by n1, n2, ..., nr, it must be true that n1 + n2 + ... + nr < m. And if this is the case, we say that toy m contains directly the toys n1, n2, ..., nr . It should be clear that toys that may be contained in any of the toys n1, n2, ..., nr are not considered as directly contained in the toy m.

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 and k, 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 toys 2 and 3. Note that the first copy of toy 2 occurs left from the second one and that the second copy contains directly a toy 1. It would be wrong to understand that the first -2 and the last 2 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 inside 9.

  • -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 than 107.

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

Sample Output

:-) Matrioshka!
:-( Try again.
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.
#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <stack>
#include <list>
#include <sstream>
using namespace std;
/*
题意:一个可以嵌套娃娃的娃娃,然后嵌套在里面的娃娃又可以继续嵌套娃娃。
然后要求直接嵌套在里面(内一层)的娃娃的尺寸大小之和不能超过外面的。
(要特别注意输入)
*/
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define N 50000
#define INF 0x3fffffff
#define vint vector<int>
#define sint set<int>
#define mint map<int, int>
#define lint list<int>
#define sch stack<char>
int seq[N], sum[N], top, a;
string line;
void init()
{
    ms(sum, 0);
    top = -1;
}

bool judge()
{
    init();
    istringstream iss(line);
    while (iss>>a)
    {
        if (a < 0)
        {
            sum[++top] -= a;//记录当前层的和
            seq[top] = a;
        }
        else
        {
            if (top < 0)//序列为空
            {
                return false;
            }
            if (-seq[top] != a)//不等
            {
                return false;
            }
            if (-seq[top] <= sum[top + 1])//当前数字不大于内层和
            {
                return false;
            }
            //满足条件,可匹配
            sum[top + 1] = 0;//内层和清空,不影响后面操作
            top--;
        }
    }
    return top < 0 ? true : false;
}
int main()
{
    while (getline(cin, line))
    {
        cout << (judge() ? ":-) Matrioshka!" : ":-( Try again.") << endl;
    }

    return 0;
}

uva 11111 - Generalized Matrioshkas

时间: 2024-12-17 02:45:21

uva 11111 - Generalized Matrioshkas的相关文章

UVa 11111 一般Matrioshka 及 scanf 处理一行

题目:nest,嵌套 思路:思路想通了其实很简单:将负数压栈,遇到输入正数时,检查栈顶元素,如果栈顶是正数,则出栈,一直检查栈顶元素直到栈顶是负数,判断该数和栈顶负数是否为相反数,并判断出栈的那些正数之和是否小于该输入的正数(这个是满足题目中的相加小于m的条件).如果满足,则栈顶负数出栈,输入的正数入栈.这样循环到最后,如果一直匹配则最后栈中只剩一个元素.    思路很清晰,但实现起来比较麻烦,主要是EOF和\n的判断.我一直以为scanf是不能用来辨别一行的,因为它会跳过空白符.看了别人的一篇

ACM学习历程——UVA11111 Generalized Matrioshkas(栈)

Description Problem B - Generalized Matrioshkas   Problem B - Generalized Matrioshkas  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 ha

Generalized Matrioshkas UVA 11111

说说: 题意就是一个大整数中包含几个小整数,且小整数的和要大于大整数.同时小整数中也可能包含其他小整数,依次类推.要求判断整个的包含关系是否正确.只是题目给的说明不是很清楚.首先给你一个序列,比如:-9     -7     -2    2     -3     -1     -2    2    1    3    7    9  其中9的范围就是-9到9之间,并且只包含了7.如果序列为-9 -7 7 -2 2 9 则9中就包含了7和2两个数字,显然这种情况是不合法的.解法的话,无非就是建立两

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVA 11111-Generalized Matrioshkas(栈)

题意:有很多层盒子,盒子里面再套盒子,一个盒子可能套多个独立的子盒子,但子盒子的总体积必须小于该盒子,否则不合法,输入给一行数,负数代表左边,正数代表右边,大小表示其体积,如-2,-1,1,2则表示体积为2的盒子里套一个体积为1的盒子,再比如-5,-2,2,-1,1,5表示体积为5的盒子套两个盒子分别为2和1,题目要求判断给出的一行数是否合法.一定要保证子盒子的体积小于大盒子.比如-5,-4,4,-2,2,5就不合法. 解析:栈的使用,但同时维护另一个值,该盒子剩余能容纳的体积,比如该盒子的体积

UVA 624 CD 记录路径DP

开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #in

状压DP UVA 11795 Mega Man&#39;s Mission

题目传送门 1 /* 2 题意:洛克人有武器可以消灭机器人,还可以从被摧毁的机器人手里得到武器,问消灭全部机器人的顺序总数 3 状态压缩DP:看到数据只有16,就应该想到状压(并没有).因为是照解题报告写的,代码里加点注释,省的以后忘记了 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 10:41:28 8 * File Nam

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te