跳到正文

题解

题解 P2866 【[USACO06NOV]Bad Hair Day S】

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

文章信息

发布
2020年6月11日
收录
2026年9月28日
更新
2026年11月11日
篇幅
约 1 分钟
获赞
428
题目
P2866

版本

  1. v1当前

题目传送门

本题考查对栈的应用

题目解法:

  1. 每次输入一头牛的身高,找比这头牛矮的,出栈

  2. 剩下的牛皆可以看到这只牛

  3. ans值加等于栈中牛的个数

  4. 这头牛入栈

Code:

C++
#include<bits/stdc++.h>
using namespace std;
int n,t;
long long ans; //注意要开long long 
stack <int> a;
int main() {
	cin>>n;
	for (int i=1; i<=n; i++) {
		cin>>t;
		while (!a.empty()&&a.top()<=t)  
			a.pop();
		ans+=a.size();
		a.push(t);
	}
	cout<<ans;
	return 0;
}

都看到这了,点个赞呗QwQ