当前位置:网站首页>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;
}
边栏推荐
- Redis (II) - redis General Command
- [shell] summary of common shell commands and test judgment statements
- Markdown displays pictures side by side
- 2022Android面试必备知识点,一文全面总结
- [opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat
- RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
- 字符串常量与字符串对象分配内存时的区别
- 力扣62 不同路径(从矩阵左上到右下的所有路径数量) (动态规划)
- ETCD数据库源码分析——从raftNode的start函数说起
- string(讲解)
猜你喜欢

tkinter窗口选择pcd文件并显示点云(open3d)

Common problems of caching in high concurrency scenarios

Cloudcompare point pair selection

【OpenCV】形态学滤波(2):开运算、形态学梯度、顶帽、黑帽

ip地址那点事

Learning notes | data Xiaobai uses dataease to make a large data screen
![Stack and queue-p79-10 [2014 unified examination real question]](/img/7f/e5bba2f64bb77e1781076361c32f01.jpg)
Stack and queue-p79-10 [2014 unified examination real question]

string(讲解)

uniapp开发小程序如何使用微信云托管或云函数进行云开发

快速定量,Abbkine 蛋白质定量试剂盒BCA法来了!
随机推荐
Postgresql中procedure支持事务语法(实例&分析)
Symmetric binary tree [tree traversal]
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
How to find the literature of a foreign language journal?
线性代数(一)
快速定量,Abbkine 蛋白质定量试剂盒BCA法来了!
【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)
UIC(组态UI工程)公版文件库新增7款行业素材
c语言(结构体)定义一个User结构体,含以下字段:
Wechat applet hides the progress bar component of the video tag
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
二十岁的我4面拿到字节跳动offer,至今不敢相信
「运维有小邓」符合GDPR的合规要求
Which foreign language periodicals are famous in geology?
Installing redis and windows extension method under win system
String (explanation)
Install mongodb database
DB2获取表信息异常:Caused by: com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.25.13]
C interview 24 (pointer) define a double array with 20 elements a
[opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat