Codeforces 547b Mike and feet (monotonous stack)

Source: Internet
Author: User

Link: B

There is a sequence with a length of N, which has a continuous subsequence with a length of 1... n. The smallest value in a continuous subsequence is called the strength of the subsequence of this subsequence,
The maximum strength of a continuous subsequence of each length is required.
You can use the stack to obtain the L [I] of each vertex, indicating that the value is smaller than the current position and closest to this point on the left.
Similarly, we can find the R [I], which means the value is smaller than the current position and closest to this point on the right side.
The process of obtaining L [I] is as follows:
Stack S // initially empty
For I = 1 to n
While S is not empty and a [S. Top ()]> = A [I]
Do S. Pop ()
If S is empty
Then l [I] = 0
L [I] = S. Top ()
S. Push (I)
Next, we traverse each position I, so that Len = R [I]-l [I]-1, then ans [Len] = min (A [I], ans [Len])
It can be understood that in R [I]-1 ~ L [I] + 1 in the Len interval, the smallest is a [I], so ans [Len] And a [I] are compared.
Finally, from Len = n-1 ~ 1 traversal, so ans [Len] = max (ANS [Len], a [Len + 1]).
This is not the case. For example, for example, 1 2 3 2 1, we can only find ans [1], ANS [3], ANS [5], for example, ANS [2] and ANS [4] are empty.
This means that if ans [Len] is empty, the value of ANS [Len + 1] is given to it.
Why is it the value of ANS [Len + 1]? First, ANS [Len]> = ans [Len + 1]. Suppose we have a length of Len + 1 and only X ~ In the range of Y, min = 2, then a [Len + 1] = 2,
Assume that a [Len] is greater than a [Len + 1], then there must be x + 1 ~ Y or X ~ The range of the Y-1 and min> 2, but if ans [len-1] is empty, it means there is no such range
Therefore, ANS [Len] = ans [Len + 1] is used.


 1 #include<bits/stdc++.h> 2 #define lc(a) (a<<1) 3 #define rc(a) (a<<1|1) 4 #define MID(a,b) ((a+b)>>1) 5 #define fin(name)  freopen(name,"r",stdin) 6 #define fout(name) freopen(name,"w",stdout) 7 #define clr(arr,val) memset(arr,val,sizeof(arr)) 8 #define _for(i,start,end) for(int i=start;i<=end;i++) 9 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);10 using namespace std;11 typedef long long LL;12 const int N=2e5+5;13 const int INF=0x3f3f3f3f;14 const double eps=1e-10;15 16 int l[N],r[N],a[N],ans[N];17 stack<int>sk;18 19 int main(){20     FAST_IO;21     int n;22     cin>>n;23     for(int i=1;i<=n;i++){24         cin>>a[i];25     }26     for(int i=1;i<=n;i++){27         while(!sk.empty()&&a[]>=a[i]) 28             sk.pop();29         if(!sk.empty())30             l[i];31         else32             l[i]=0;33         sk.push(i);34     }35     while(!sk.empty()) sk.pop();36     for(int i=n;i>=1;i--){37         while(!sk.empty()&&a[]>=a[i])38             sk.pop();39         if(!sk.empty())40             r[i];41         else42             r[i]=n+1;43         sk.push(i);44     }45     for(int i=1;i<=n;i++){46         int len=r[i]-l[i]-1;47         ans[len]=max(a[i],ans[len]);48     }49     for(int i=n-1;i>=1;i--){50         ans[i]=max(ans[i],ans[i+1]);51     }52     for(int i=1;i<=n;i++){53         cout<<ans[i]<<" ";54     }55     cout<<endl;56     return 0;57 }


Codeforces 547b Mike and feet (monotonous stack)

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