UVA 11111 Generalized Matrioshkas
来源:互联网 发布:网络暴力的危害 编辑:程序博客网 时间:2024/05/29 16:32
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:
For example, the sequence
On the other hand, the following sequences do not describe generalized matrioshkas:
- -9 -7 -2 2 -3 -1 -2 2 1 3 7 9because 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 9because 7 and 2 may not be allocated together inside 9.
- -9 -7 -2 2 -3 -1 -2 3 2 1 7 9because 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.
题意: 给定一些玩具,每个玩具都是拆成2份,比如-9 和 9 代表一个容量9的玩具。 玩具里面可以套小的玩具。。 只要容量足够。。要求给定序列能不能成功组装。。
思路: 用栈的思想。 这题等同于括号平衡的高级版。。多一个容量的判断即可。。
#include <stdio.h>#include <string.h>#include <stdlib.h>struct S{ int stack; long long value;} s[100005];int save[100005];int save_n = 0;int sb;char c;int top = 0;int judge = 0;int main(){ memset(s, 0 , sizeof(s)); memset(save, 0 , sizeof(save)); s[0].stack = 999999999; while (scanf("%d%c", &save[save_n ++], &c) != EOF) {if (c == '\n'){ for (int i = 0; i < save_n; i ++) {if (save[i] + s[top].stack == 0){ s[top --].stack = 0; s[top].value += abs(save[i]); if (s[top].value >= abs(s[top].stack)) {judge = 1;break; }}else{ top ++; s[top].stack = save[i]; s[top].value = 0;} } if (top)judge = 1; if (judge)printf(":-( Try again.\n"); elseprintf(":-) Matrioshka!\n"); memset(save, 0 , sizeof(save)); memset(s, 0 , sizeof(s)); s[0].stack = 999999999; save_n = 0; judge = 0; top = 0;} } return 0;}
- uva 11111 - Generalized Matrioshkas
- UVa 11111 Generalized Matrioshkas
- uva 11111 - Generalized Matrioshkas
- uva 11111 - Generalized Matrioshkas
- UVa 11111 - Generalized Matrioshkas
- uva 11111 - Generalized Matrioshkas
- UVa 11111 - Generalized Matrioshkas
- UVA-11111-Generalized Matrioshkas
- uva 11111 - Generalized Matrioshkas
- uva-11111 Generalized Matrioshkas
- UVa 11111 - Generalized Matrioshkas
- UVA 11111 Generalized Matrioshkas
- uva 11111 Generalized Matrioshkas
- Generalized Matrioshkas UVA 11111
- UVa:11111 Generalized Matrioshkas
- UVa 11111 Generalized Matrioshkas
- UVA 11111 - Generalized Matrioshkas
- uva 11111 - Generalized Matrioshkas
- ajax实现步骤
- hdu 3496 Watch The Movie (二维背包简单题)
- JAVA学习十八:String类、StrinBuffer类
- 天梯 1160 蛇形矩阵
- cocos 事件分发2
- UVA 11111 Generalized Matrioshkas
- IP地址格式转换
- JAVA学习十九:集合框架
- Hive的JOIN用法
- MySQL 优化Limit分页
- C++中STL容器的find的应用
- 几种简单的排序算法
- uva10494 (高精度除法)
- udhcp源码详解 续