提交时间:2024-04-06 21:31:59

运行 ID: 142290

#include<bits/stdc++.h> using namespace std; typedef long long LL; struct asd{ int w; LL Left; //就是表它向右的答案 LL cet; //右边重复个数 }; int n,x; stack<asd>q; LL ans; int main() { while(!q.empty()) q.pop(); scanf("%d",&n); asd now,nex; ans=0; for(int i=1;i<=n;i++) { scanf("%d",&x); now.Left=0; //都有它一个。 now.cet=0; now.w=x; if(q.empty()) q.push(now); else { if(q.top().w>=now.w) q.push(now); else { while(!q.empty()&&q.top().w<now.w) { nex=q.top();q.pop(); ans=ans+nex.Left+1LL; //直接+答案咯 if(!q.empty()) { q.top().Left+=nex.cet+1LL; if(q.top().w==nex.w) q.top().cet+=nex.cet+1LL; } //printf("%d %lld\n",x,ans); } q.push(now); } } } while(!q.empty()) { nex=q.top();q.pop(); if(!q.empty()) { q.top().Left+=nex.cet+1LL; if(q.top().w==nex.w) q.top().cet+=nex.cet+1LL; } ans=ans+nex.Left; } printf("%lld\n",ans); return 0; }