Ultraviolet A 11111-generalized matrioshkas

Source: Internet
Author: User
Va 11111-generalized matrioshkastable OF CONTENTS
  • 1. Question
  • 2 ideas
  • 3 code
  • 4. Reference
1. Question

==============================

Problem B-generalized matrioshkas


Vladimir worked for years makingMatrioshkas, 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 shocould be constructed. A toy is represented with a positive integer, according to its size. More precisely: If when opening the toy represented
ByMWe find the toys representedN1,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 shoshould be clear that toys that may be contained in any of the toysN1,N2 ,...,NR are
Not considered as directly contained in the toyM.

AGeneralized MatrioshkaIs denoted with a non-empty sequence of non zero Integers of the form:

A1A2...AN

Such that toyKIs represented
In the sequence with two integers-KAndK,
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 wocould be wrong to understand that the first-2 and
The last 2 shoshould be too red.

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 shoshould 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.

==============================

2 ideas

Because data is nested on one layer, two stacks are required. One stack processes the current data, and the other stack records the sum of the corresponding items, to verify whether N1 + N2 +... + NR <m.

3 code
/* * Problem: 11111 - Generalized Matrioshkas * Lang: ANSI C * Time: 0.062s * Author: minix */#include <stdio.h>#include <string.h>#define N 10000int d[N], s[N], sum[N], top;int main() {  int i, n, num, flag;  char c;  while (scanf ("%d", &d[0]) != EOF) {    c = getchar();    n = 1; top = -1; flag = 1;    memset (sum, 0, sizeof(sum[0])*N);    while (c != '\n') {      scanf ("%d", &d[n]);      c = getchar(); n++;    }    for (i=0; i<n; i++) {      if (d[i] < 0) {s[++top] = d[i];}      else {        if (top<0 || s[top] != -1*d[i] || d[i]<=sum[top]) {flag = 0; break;}        else {          sum[top] = 0;          top--;          if (top >=0)            sum[top] += d[i];        }      }    }    if (top== -1 && flag == 1) printf (":-) Matrioshka!\n");    else printf (":-( Try again.\n");  }  return 0;}
4 reference 1. http://uva.onlinejudge.org/external/111/11111.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.