核心思路:
与其求一头牛能看见几头牛,不如反过来,计算一头牛能被几头牛看见。由于是要求总数,因此不难想到,能看见的总数一定等于被看见的总数。
让你维护一个严格递减的序列,显然是单调栈了。栈也是严格递减的。
ACcode:
#include<bits/stdc++.h>
using namespace std;
int n,x;
stack<int>st;
long long ans;
void solve() {
cin>>n;
while(n--){
cin>>x;
if(!st.empty()&&st.top()<=x) st.pop();
ans+=st.size();
st.push(x);
}
cout<<ans<<"\n";
}
int main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
solve();
return 0;
}
over~