Click to climb the mountain
Basic exercises of monotonic stacking
However, people are silly, thinking it is complicated, knowing that it is monotonic stacking, but in Before looking at Qer’s answer to the question, I used the monotonic stack to maintain the total number of peaks that can be seen by each mountain. There is also a stack that maintains the suffix and? ? ? ? (Mdzz)
Anyway, I am mentally retarded.
Obviously, I only need to maintain the current monotonously decreasing mountain sequence.
Kneel to the great god Qer’
The code is as follows< /p>
#include #include < span class="hljs-preprocessor">#include #include #include < stack>#define maxn 15005using namespace std;typedef long long LL;LL n;LL sum,ans;LL num[maxn];stack s;void rd(LL &x){ scanf("%lld",&x);}inline void dosomething(){ for(LL i = 1;i <= n;i++) {rd(num[i]); if(s.empty()){s.push(num[i]) ;} else {sum++; ans += sum; while(s.size()) {if(s.top() else span> break;} s.push(num[i]);}} printf("%lld
",ans);}int main(){ rd(n); dosomething(); return 0;}
By Peacefuldoge
http://blog.csdn.net/loi_peacefuldog< /p>
Click to climb the mountain
Basic exercises of monotonic stacking
However, people are silly, thinking complicated, knowing that it is monotonic stacking , But before looking at Qer’s solution to the problem, I use the monotonic stack to maintain the total number of peaks that can be seen by each mountain. There is also a stack that maintains the suffix and? ? ? ? (Mdzz)
Anyway, I am mentally retarded.
Obviously, I only need to maintain the current monotonously decreasing mountain sequence.
Kneel to the great god Qer’
The code is as follows< /p>
#include #include < span class="hljs-preprocessor">#include #include #include < stack>#define maxn 15005using namespace std;typedef long long LL;LL n;LL sum,ans;LL num[maxn];stack s;void rd(LL &x){ scanf("%lld",&x);}inline void dosomething(){ for(LL i = 1;i <= n;i++) {rd(num[i]); if(s.empty()){s.push(num[i]) ;} else {sum++; ans += sum; while(s.size()) {if(s.top() else span> break;} s.push(num[i]);}} printf("%lld
",ans);}int main(){ rd(n); dosomething(); return 0;}
T HE END
By Peacefuldoge
http://blog.csdn.net/loi_peacefuldog