Bad Hair Day POJ - 3250(单调栈模板)

题目链接

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
int s[maxn],tt=0,a[maxn];
main() {
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)cin>>a[i];
	int ans=0;
	//n++;a[n]=inf;
	for(int i=1; i<=n; i++) {
		//cout<<123<<endl;
		while(tt&&a[i]>=s[tt])tt--;
		ans+=tt; 
		s[++tt]=a[i];
	}
	cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/Alanrookie/article/details/107213878
今日推荐