璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:usaco22jan_cow_frisbee_s
problems
名称Cow Frisbee S
题目编号USACO22JAN_S2
题目链接luogu.com.cn/…
来源USACO
算法分类单调栈
难易程度容易

Cow Frisbee S

想法

维护一个从i往左看,能看到的栈,也就是单调递减的栈。

代码实现

#include<iostream>
#include<stack>
using namespace std;
 
int main()
{
	long long int n, temp, ans = 0;
	cin >> n;
	stack<pair<int, long long int> >s;
	for (int i = 0; i < n; ++i) {
		cin >> temp;
		while((!s.empty()) && s.top().second <= temp)
		{
			ans += i - s.top().first + 1;
			s.pop();
		}
		if(!s.empty())
			ans += i - s.top().first + 1;
		s.push(make_pair(i, temp));
	}
 
	cout << ans;
	return 0;
}

板书

/app/www/public/data/pages/icpc/problems/usaco22jan_cow_frisbee_s.txt · 最后更改: 2023/02/08 08:25 由 温婕莺