CodeForces547 B.Mike and Feet (monotone stack / dp)

Meaning of the questions:

A given length n of the array, n <= 2e5
define F (l, r) is a minimum number of the interval [l, r] is, ans (k) of max (F (l, l + k-1)), wherein 1 <= l <= nk, i.e. ANS (k) is the maximum length of a section F k.
Now the required output order of 1 to k n, ANS (k) value

Ideas:

With L (i) and R (i) recording a minimum value at the i-th left and right boundaries can be extended, it is possible to try to update ans (1) to ANS (R (i) with a (i) -L ( i) +1),
because in the interval [L (i), R ( i)] comprises the a (i) are the minimum interval is a (i).
Each first with a (i) Update ans (R (i) -L ( i) +1), the last on the list recursion: ans (i) = max ( ans (i), ans (i + 1)) , ditto (a large interval contains inter-cell).

Calculating L (i), R (i) is easy to think monotony stack, dp can write (see the code).

code(dp):

#include<bits/stdc++.h>
using namespace std;
const int maxm=2e5+5;
int l[maxm],r[maxm];
int a[maxm];
int ans[maxm];
signed main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    a[0]=a[n+1]=-1;
    for(int i=1;i<=n;i++){
        int j=i-1;
        while(a[i]<=a[j])j=l[j];
        l[i]=j;
    }
    for(int i=n;i>=1;i--){
        int j=i+1;
        while(a[i]<=a[j])j=r[j];
        r[i]=j;
    }
    for(int i=1;i<=n;i++){
        l[i]++;
        r[i]--;
    }
    for(int i=1;i<=n;i++){
        int len=r[i]-l[i]+1;
        ans[len]=max(ans[len],a[i]);
    }
    for(int i=n-1;i>=1;i--){
        ans[i]=max(ans[i],ans[i+1]);
    }
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<' ';
    }
    return 0;
}
Published 430 original articles · won praise 36 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/104688275