30分求助
查看原帖
30分求助
1104834
lucky213楼主2024/11/11 19:21
#include<bits/stdc++.h>
using namespace std;
long long n,a[1010000],st[1010000],top,s[1010000],cnt;
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		while(top>0&&a[st[top]]<a[i]){
			s[st[top]]=i;
			st[top]=0;
			top--;
		}
		st[++top]=i;
	}
	for(int i=1;i<=n;i++){
		if(s[i]!=0)
			cnt+=(s[i]-i-1);
		else{
			if(i!=n){
				cnt+=n-i;
			}
		}
	}
	printf("%lld",cnt);
	return 0;
}
2024/11/11 19:21
加载中...