洛谷P1165 日志分析 栈

洛谷P1165 日志分析

在这道题中 栈st[ i ] 表示的有点特殊
st[ i ] 表示的st[ 1 ]--st[ i ] 中最大的值

 

 1 #include <bits/stdc++.h> 
 2 #define For(i,j,k) for(int i=j;i<=k;i++) 
 3 using namespace std ; 
 4 
 5 const int N = 200011 ; 
 6 int Q,type,top,v ; 
 7 int st[N] ; 
 8 
 9 inline int read() 
10 {
11     int x = 0 , f = 1 ; 
12     char ch = getchar() ; 
13     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar(); } 
14     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar(); } 
15     return x * f ; 
16 }
17 
18 int main() 
19 {
20     Q = read() ; 
21     while(Q--) {
22         type = read() ;  
23         if(type==0) {
24             st[++top] = read() ; 
25             st[top] = max(st[top],st[top-1]) ; 
26         }
27         else if(type==1) top = max(0,top-1) ;   // 防止 栈空了还在不停退栈 
28         else printf("%d\n",st[top]) ; 
29     }
30     
31     return 0 ; 
32 }

 

posted @ 2017-07-26 08:24  third2333  阅读(179)  评论(0编辑  收藏  举报