[P1165] log analysis data structure

Title Description

M M shipping company has recently decided to statistics of the goods out of its warehouse. The only record they have is a record of log container out of the situation. This log records the two types of operations: a first operation type of operation is a storage container, and the weight of the secondary storage container; a second type of operation is an operation of the library container. These records are in strict chronological order. Rules inbound and outbound container is advanced after that every time a library operating a container library is currently in a warehouse all the latest storage containers in the container.

For analytical purposes, in the log analysts randomly inserted several third type operation - query operation. When analyzing the log, when confronted with a query operation, must report the weight of the current warehouse in the largest containers.

Input Format

It contains N + . 1 line:

The first acts a positive integer N the total number N, corresponding to the operation log contained within.

The following N lines belonging to one of three formats:

Format . 1:  0   X // a container storage operation, a positive integer and X represents the weight of the storage container of times

Format 2:  1 // one container out of library operations, (it was in terms of) the final storage container out of the library

Format 3:  2 // a query, analysis program output requirements within the current weight of the largest container warehouse

When the warehouse is empty, you should ignore the library operation, when the warehouse is empty query you should output 0 .

Output Format

The number of rows equals the number of output log query operation. Each behavior a positive integer representing the query results.

Sample input and output

Input # 1
13
0 1
0 2
2
0 4
0 2
2
1
2
1
1
2
1
2
Output #
2
4
4
1
0



【answer】:

  This process is achieved using two stacks.

  The first stack is the current stored value of the other stack for storing the current maximum value.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 stack<int> St_val , St_max ;
 4 int main()
 5 {
 6     int n,opt,x;
 7     scanf("%d",&n);
 8     for(int i=0;i<n;i++){
 9         scanf("%d",&opt);
10         if( opt == 0 ){
11             scanf("%d",&x);
12             St_val.push(x);
13             if( St_max.empty() || St_max.top() < x )
14                 St_max.push(x);
15         }else if( opt == 1 ){
16             if( St_val.empty() ) continue ;
17 
18             if( St_val.top() == St_max.top() ){
19                 St_max.pop();
20             }
21             St_val.pop();
22         }else{
23             if( St_val.empty() ){ puts("0");continue ;}
24 
25             printf("%d\n",(int)St_max.top() );
26         }
27     }
28 
29     return 0 ;
30 }
View Code

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Osea/p/11410322.html