提交时间:2022-07-20 12:43:54

运行 ID: 52985

#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn=1e6+5; int a[maxn],ans[maxn],n; int id[maxn]; bool cmp(int i,int j){ return a[i]>a[j]; } int front; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),id[i]=i; sort(id+1,id+n+1,cmp); front=n+1; for(int j=1;j<=n;j++){ int i=id[j]; int s=j;while(a[id[j+1]]==a[i]&&j+1<=n) j++; //cout<<s<<" "<<j<<endl; for(int x=s;x<=j;x++){ if(front>id[x]) continue; ans[id[x]]=id[x]-front; } for(int x=s;x<=j;x++){ front=min(front,id[x]); } } ll sum=0; for(int i=1;i<=n;i++){ sum+=ans[i]; //cout<<ans[i]<<endl; } printf("%lld",sum); }