洛谷P1165 日志分析 栈

本文介绍了解决洛谷P1165日志分析栈问题的一种方法,通过特殊的栈实现来记录操作过程中的最大值。使用C++实现,包括读取输入、栈操作和输出结果等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

洛谷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 }

 

转载于:https://www.cnblogs.com/third2333/p/7237486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值