提交时间:2022-07-20 12:01:09

运行 ID: 52815

#include<bits/stdc++.h> using namespace std; const int N=1e6+5; struct NODE { int id; int h; }; int n; priority_queue<NODE>G; bool vis[N]; int a[N]; long long ans; bool operator<(const NODE& a,const NODE& b) { return a.h>b.h; } int main() { scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); G.push(NODE{i,a[i]}); } for(int i=1;i<=n;++i) { if(vis[i])continue; while(G.top().h<a[i]) { NODE v=G.top(); G.pop(); if(v.id<=i)continue; ans+=(long long)v.id-i; vis[v.id]=true; } } printf("%d\n",ans); }