当前位置:网站首页>Leetcode T1165: 日志分析
Leetcode T1165: 日志分析
2022-07-07 02:24:00 【范谦之】
日志分析
题目描述
M M M 海运公司最近要对旗下仓库的货物进出情况进行统计。目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志。该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装箱重量;第二类操作为集装箱的出库操作。这些记录都严格按时间顺序排列。集装箱入库和出库的规则为先进后出,即每次出库操作出库的集装箱为当前在仓库里所有集装箱中最晚入库的集装箱。
出于分析目的,分析人员在日志中随机插入了若干第三类操作――查询操作。分析日志时,每遇到一次查询操作,都要报告出当前仓库中最大集装箱的重量。
输入格式
包含 N + 1 N+1 N+1 行:
第一行为 1 1 1 个正整数 N N N,对应于日志内所含操作的总数。
接下来的$N $行,分别属于以下三种格式之一:
格式 1 1 1: 0 X 0 X 0X //一次集装箱入库操作,正整数 X X X表示该次入库的集装箱的重量
格式 2 2 2: 1 1 1 //一次集装箱出库操作,(就当时而言)最后入库的集装箱出库
格式 3 3 3: 2 2 2 //一次查询操作,要求分析程序输出当前仓库内最大集装箱的重量
当仓库为空时你应该忽略出库操作,当仓库为空查询时你应该输出 0 0 0。
输出格式
输出行数等于日志中查询操作的次数。每行为一个正整数,表示查询结果。
样例 #1
样例输入 #1
13
0 1
0 2
2
0 4
0 2
2
1
2
1
1
2
1
2
样例输出 #1
2
4
4
1
0
提示
对于 20 % 20\% 20%的数据,有 N ≤ 10 N≤10 N≤10;
对于 40 % 40\% 40%的数据,有 N ≤ 1000 N≤1000 N≤1000;
对于 100 % 100\% 100%的数据,有 N ≤ 200000 , X ≤ 1 0 8 N≤200000,X≤10^8 N≤200000,X≤108。
思路
此题在于维护一个栈a[],实现它的 进栈、出栈、查询操作,以满足题目的要求。该栈需满足 a[i] 为栈底到 i 个元素中最大的。
- 入栈:当入栈一个元素 x,t++,a[t] = max(a[t-1], x)
- 出栈:t–
- 查询:输出a[t]
上述栈可以满足题目要求的关键在于:如果进入的一个元素 x 并不是当前栈内最大的(假设最大的为mx),即之前进入的元素有比它大的,那么我们不关心x的值到底是多少,因为直到它被出栈,也不会被访问,因为访问数会输出mx。如下图:
并且,栈底元素为0,满足题目要求:当仓库为空时输出0.
代码
#include<iostream>
#include<cmath>
using namespace std;
int a[200001], t = 0, y, n, ord;
int main() {
a[0] = 0;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> ord;
if(ord == 0) {
cin >> y;
t+=1;
a[t] = max(a[t-1], y);
}
else if(ord == 1) {
if(t > 0) t-=1;
}
else cout << a[t] << endl;
}
return 0;
}
边栏推荐
- 安装mongodb数据库
- SVN version management in use replacement release and connection reset
- 力扣62 不同路径(从矩阵左上到右下的所有路径数量) (动态规划)
- Symmetric binary tree [tree traversal]
- 基本Dos命令
- Install mongodb database
- How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet
- 【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽
- Cloudcompare point pair selection
- Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
猜你喜欢
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!
ICML 2022 | 探索语言模型的最佳架构和训练方法
ICML 2022 | explore the best architecture and training method of language model
途家、木鸟、美团……民宿暑期战事将起
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
[opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat
2022 Android interview essential knowledge points, a comprehensive summary
dolphinscheduler3.x本地启动
Jmeter 5.5版本发布说明
Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading
随机推荐
途家、木鸟、美团……民宿暑期战事将起
Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
DB2获取表信息异常:Caused by: com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.25.13]
Kotlin之 Databinding 异常
Problems and precautions about using data pumps (expdp, impdp) to export and import large capacity tables in Oracle migration
Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
ESXI挂载移动(机械)硬盘详细教程
FlexRay通信协议概述
ViewModelProvider.of 过时方法解决
ICML 2022 | 探索语言模型的最佳架构和训练方法
[opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat
Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
Abnova 免疫组化服务解决方案
LM11丨重构K线构建择时交易策略
Redhat5 installing vmware tools under virtual machine
How to find the literature of a foreign language journal?
Can't you really do it when you are 35 years old?
c语言面试写一个函数在字符串N中查找第一次出现子串M的位置。
博士申请 | 上海交通大学自然科学研究院洪亮教授招收深度学习方向博士生
哈趣投影黑馬之姿,僅用半年强勢突圍千元投影儀市場!