Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 2052
Question meaning:
As the problem statement is too long, I haven't read the problem statement completely during the contest, consider you have some boxes that some of them are nested in some other boxes, each box shoshould begin with negative
Number and end with the absolute value of that negative number,The absolute value shows the size of box, and the sum of size of inside boxes shocould be less than the outside boxes, You are given sequence of the numbers, you should check
Whether it violates the above rules or not, that's all. Hope it helps!
Solution:
A stack is used for simulation. When a negative number is encountered, the stack encounters positive number processing. If the elements complement each other at the top of the current stack, and the sum of the elements at the top of the stack is smaller than the current positive number, the output stack meets the requirements, and the value is added to the "and" at the top of the current stack. For details, see the code.
Code:
# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # define EPS 1e-6 # define Inf (1 <20) # define PI ACOs (-1.0) using namespace STD; struct stack {int top, flag; // int save [200] [2]; // save [I] [0] indicates the value of the current negative number, and save [I] [1] indicates the sum of the domains under which the current negative number belongs. Void push (INT N) {++ top; save [Top] [0] = N; // initialize save [Top] [1] = 0;} int Pop () {return save [top --] [0]; // output stack} bool empty () {If (top) return false; return true;} void set () {Top = Flag = 0 ;}} stack; int main () {int cur; while (scanf ("% d", & cur )! = EOF) {stack. set (); int last = cur; char temp = getchar (); If (cur <0) stack. push (cur); else // if it is a positive number at the beginning, it is certainly not a stack. flag = 1; while (temp! = '\ N') {scanf ("% d % C", & cur, & temp); If (cur <0) // if it is a negative number, directly press the stack. push (cur); else {int temptop = stack. top; If (stack. save [temptop] [0] + cur = 0) // if it is complementary to the current top element of the stack {If (stack. save [temptop] [1] <cur) // if the "and" at the top of the current stack are smaller than the current value, the requirements are met {stack. pop (); If (! Stack. empty () stack. save [stack. top] [1] + = cur;} // The else stack does not meet the requirements. flag = 1;} else // stack not meeting requirements. flag = 1 ;}} if (stack. flag = 1 |! Stack. Empty () printf (":-(try again. \ n"); else printf (":-) Matrioshka! \ N ") ;}return 0 ;}