"Rqnoj" 460 Connaught's queue

Source: Internet
Author: User

"The main topic"
All number pairs (i,j) satisfy any a[k]<=a[i] and a[k]<=a[j].
Figuratively speaking, there is a group of people standing in a row, each person has a certain height, and then asked how many people can see each other.
The number pairs (I,J) are simply referred to as the number pairs that can be seen.

"Parsing" monotonic stacks
First, borrow the previous question: [vijos]1926 purple bracelet, the maximum value of any interval maximum value of the largest difference or the second large value.
Reviewing the monotony stack is the stack that stores the monotonically data from high to low.
To borrow the previous practice, the things that are found are relative to all the visible pairs, for all A[i] equal to the number of visible pairs, only to forget once.

So in fact each time higher than others when the operation just add S (a[i]), the first time shorter than others with 1 instead of S (A[i]).
To expand the contents of the stack, not only the elements, but also save the number, so that the solution.

Monotonic Stack Code:

#include <cstdio> #include <cstring> #include <cstdlib>using namespace Std;const int n=500001;struct Stack{int W,c;} Stk[n];int size;int n,w[n],cnt;inline int read (void) {int s=0,f=1; char C=getchar (); for (;c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') f=-1;for (; ') 0 ' <=c&&c<= ' 9 '; C=getchar ()) s= (s<<1) + (s<<3) +c-' 0 '; return s*f;} int main (void) {n=read (); for (int i=1;i<=n;i++) W[i]=read (), for (int. i=1;i<=n;i++) {for (; Size&&stk[size] . w<w[i];size--) {cnt+=stk[size].c;stk[size].w=stk[size].c=0;} if (Size&&stk[size].w==w[i]) {cnt+=stk[size].c;stk[size].c++;if (size-1) cnt++;} Else{if (size) cnt++;stk[++size].w=w[i];stk[size].c=1;}} printf ("%d\n", CNT); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Rqnoj" 460 Connaught's queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.