提交时间:2024-04-06 15:02:13
运行 ID: 141942
#include<iostream> #include<cstdio> #include<stack> #include<queue> using namespace std; const int R=2e6; stack<int> stk; long long ans=0; int n,h,bucket[R]; void lss() { ans++; //即使bucket[stk.top()]不只一个,也只能看到一个,例如8 8 6,6只能看到一个8 stk.push(h); bucket[h]++; // cout<<"ans="<<ans<<endl; // cout<<"push "<<h<<endl; // cout<<"bucket "<<h<<" "<<bucket[h]<<endl; return; } void eqal() { ans+=bucket[h]; // cout<<"ans="<<ans<<endl; if(stk.size()>1) { // cout<<"pop "<<stk.top()<<endl; stk.pop(); lss(); } else { bucket[h]++; // cout<<"bucket "<<h<<" "<<bucket[h]<<endl; } return; } void grat() { while(!stk.empty() && h>stk.top()) { ans+=bucket[stk.top()]; bucket[stk.top()]=0; // cout<<"ans="<<ans<<endl; // cout<<"bucket "<<stk.top()<<" "<<bucket[stk.top()]<<endl; // cout<<"pop "<<stk.top()<<endl; stk.pop(); } if(!stk.empty() && h==stk.top()) eqal(); else if(!stk.empty()) lss(); else { stk.push(h); bucket[h]++; // cout<<"push "<<h<<endl; // cout<<"bucket "<<h<<" "<<bucket[h]<<endl; } return; } int main() { int i; cin>>n>>h; stk.push(h); bucket[h]=1; // cout<<"push "<<h<<endl; // cout<<"bucket "<<h<<" "<<bucket[h]<<endl; for(i=2;i<=n;i++) { cin>>h; // cout<<"cin>>"<<h<<endl; if(!stk.empty() && h<stk.top()) lss(); else if(!stk.empty() && h==stk.top()) eqal(); else if(!stk.empty()) grat(); else { stk.push(h); bucket[h]++; // cout<<"push "<<h<<endl; // cout<<"bucket "<<h<<" "<<bucket[h]<<endl; } } cout<<ans; return 0; }