单调栈。ans开long long。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 80050 using namespace std; int n,a[maxn],stack[maxn],top=0; long long ans=0; int main() { scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=n;i>=1;i--) { while ((top) && (a[i]>a[stack[top]])) top--; if (top) ans+=stack[top]-i-1; else ans+=n-i; stack[++top]=i; } printf("%lld\n",ans); return 0; }
时间: 2024-10-10 12:18:09