Luogu 题解 P1165 【日志分析】
前言
- 作者昨天废了(
别问,问就是作业煤写完
正文
原题(依旧是水题
基本思路
- 将仓库看做 栈;
0 X为入栈;1出栈;2得出 栈 中最大值;
int stack[10005];
void push(int x) { stack[++top] = x; } // 入栈
void pop() { top--; } // 出栈
void calc() { maxn = max(maxn, stack[top--]; } // 得出最大值
当作者快乐地敲着代码时,突发奇想:“如果栈里所储存的都是最大值呢?”。
带着疑问,作者在草稿纸上演示几遍,得出完整代码(作者蒟蒻。
完整代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX = 200005;
int n, ope, top, maxn, weg;
int max_stack[MAX];
void calc()
{
if (ope == 0)
{
scanf("%d", &weg);
top++;
max_stack[top] = max(max_stack[top-1], weg); // 将每次输入的值与栈内前一个值max操作,能使栈内值从左往右递增
}
else if (ope==1 && top!=0)
top--; // 出栈
else
printf("%d\n", max_stack[top]); // 因上操作,可直接输出
}
void input()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &ope);
calc();
}
}
int main()
{
// freopen("P1165.in", "w", stdout);
input();
// fclose(stdout);
return 0;
}
于是一道水题就华丽丽地被AC了。
妙极了

浙公网安备 33010602011771号