Uva_11995
If a data structure is still possible, you can simulate the operation on it until the error occurs.
# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Define Maxd 1010 Int N, D, Max [ 4 * Maxd], Q [maxd], s [maxd]; Void Init (){ For (D = 1 ; D <n + 2 ; D <= 1 ); Memset (max, 0 , Sizeof (Max [ 0 ]) * 2 * D );} Void Update ( Int I ){ For (; I ^ 1 ; I >>=1 ) Max [I > 1 ] = STD: max (MAX [I], Max [I ^ 1 ]);} Void Pop (){ Int I; For (I = 1 ; I < D ;){ If (Max [I < 1 ] = MAX [I]) I <=1 ; Else I = I < 1 | 1 ;} MAX [I] = 0 , Update (I );} Void Solve (){ Int I, op, X, front, rear, top, ISQ, ISS, ISP; front = Rear = Top = 0 ; ISQ = ISS = ISP =1 ; For (I = 0 ; I <n; I ++ ) {Scanf ( " % D " , & OP ,& X ); If (OP = 1 ){ If (ISQ) Q [rear ++] = X; If (ISS) s [top ++] = X; If (ISP) Max [D + I] = x, update (D + I );} Else { If (ISQ ){ If (Front = rear) ISQ = 0 ; Else { If (Q [Front]! = X) ISQ =0 ; ++ Front ;}} If (ISS ){ If (Top = 0 ) ISS = 0 ; Else { -- Top; If (S [Top]! = X) ISS =0 ;}} If (ISP ){ If (Max [ 1 ]! = X) ISP = 0 ; Else Pop ();}}} If (ISQ + ISS + ISP> 1 ) Printf ( " Not sure \ n " ); Else If (ISQ + ISS + ISP = 0 ) Printf ( " Impossible \ n " ); Else Printf ( " % S \ n " , ISQ? " Queue " : (ISS? " Stack " : " Priority queue " ));} Int Main (){ While (Scanf ( " % D " , & N) = 1 ) {Init (); solve ();} Return 0 ;}