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了。

posted @ 2020-04-23 21:58  Rnin  阅读(173)  评论(0)    收藏  举报