Test instructions: There are many layers of boxes, boxes inside the box, a box may be a plurality of separate sub-boxes, but the overall product of the sub-box must be less than the box, otherwise illegal, input to the number of lines, negative numbers for the left, positive numbers for the right, size represents its volume, such as -2,- The 1,1,2 represents a box with a volume of 1 in a box of 2, or a box with a volume of 5, two boxes for 2 and 1 respectively, and the question asks whether the number of rows given is legal. Make sure that the size of the child box is smaller than the large box. For example, -5,-4,4,-2,2,5 is not legal.
Parsing: The use of the stack, but also maintain another value, the box can hold the volume, such as the Box's volume is 5, has a volume of 3 box, minus 3 left 2, if there is a child box, then the volume of the child box must be less than 2, otherwise illegal.
The code is as follows:
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<set>#include<map>#include<queue>#include<vector>#include<iterator>#include<utility>#include<sstream>#include<iostream>#include<cmath>#include<stack>UsingNamespace Std;Constint INF=1000000007;ConstDouble EPS=0.00000001;int go;int My_stack[40000],leave[40000],elem[40000]; Stack, leave[] represents the remaining capacity of the boxboolSolve(){int rear=0;Memset(Leave,0,sizeof(Leave)); Leave[0]=inf;For(int I=0; I<go; I+ +)//sweep again{If(Elem[I]<0)//Less than 0 is added to the stack{Rear++; My_stack[Rear]=Abs(Elem[I]); Leave[Rear]=Abs(Elem[I]); Leave[] is equal to his value at this time.}Else{If(Rear==0)ReturnFalse; It's not done yet. Stack emptyIf(Elem[I]!=my_stack[Rear])ReturnFalse; Not the same as the top element of the stackIf(Elem[I]>=leave[Rear-1])ReturnFalse; Greater than or equal to the large box of leave[] Leave[Rear-1]-=elem[I]; Lose rear--; Out of the stack}}return rear==0; Finally, you have to decide whether the stack is empty}IntMain(){StringLine;While(Getline(CIN, line)){Istringstream Output(Line); string stream input, which makes it more convenientint a; Go=0;While(Output>>a){Elem[Go++]=a;}If(Go%2){Printf(":-( Try again.\ n "); continue; }//Not even affirmative illegal if ( solve ()) printf (\n "); else printf ( ":-( Try again. \n "); return 0;< Span class= "Sh-cbracket" >
UVA 11111-generalized Matrioshkas (Stack)