跳到正文

题解

题解:AT_donuts_2015_3 行列のできるドーナツ屋

Brute_Force约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年3月4日
更新
2026年3月4日
篇幅
约 1 分钟

版本

  1. v1当前

题意

题面太复杂了,我们先简化一下题面:

有 NN 个人在排队,第 ii 个人的身高是 HiH_i,输出从前往后到第 ii 个人由高到低的有多少个人,即题面所说的「不安度」。

看到简化体面中的“越来越高”,就说明我们需要输出容器中从大到小的数列的长度。不难发现,容器是单调栈,接下来,只需要满足栈中数是从大到小的即可。

代码

C++
#include <bits/stdc++.h>
using namespace std;
stack <int> s;
int a[100005], n;
int main () {
	ios :: sync_with_stdio (0);
	cin.tie (0), cout.tie (0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		cout << s.size () << endl;
		while (s.size () && s.top () < a[i]) s.pop ();
		s.push (a[i]);
	}
	return 0;
}
原文