跳到正文

题解

AT_donuts_2015_3 题解

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

文章信息

发布
2024年8月2日
收录
2025年12月12日
更新
2026年9月27日
篇幅
约 1 分钟
获赞
6

版本

  1. v1当前

题目传送门

题目大意

有 NN 个人在排队,第 ii 个人的身高是 HiH_i,请求出第 ii 个人向前能看到多少个人(需满足依次越来越高)。

思路

考虑使用单调栈。用单调栈维护一个身高递减的序列,第 ii 个答案就是当前栈的长度。

AC CODE

C++
#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=1e5+10;
int a[N];
stack<int>st;
int main(){
	int n=read();
	for(int i=1;i<=n;++i){
		a[i]=read();
		printf("%llu\n",st.size());
		while(!st.empty()&&st.top()<a[i])
			st.pop();
		st.push(a[i]);
	}
	return 0;
}
原文