[Data Structure] CODE [VS] 1531 Mountain Peak (single monoton maintenance monotonic delivery sequence)

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 break;} s.push(num[i]);}} printf("%lld
",ans);}int main(){ rd(n); dosomething(); return 0;}

THE END

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 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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5011 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.