Luogu2422 good feeling (monotonous stack)

Title Description

kkk made a human feeling analyzer. Every day, people have a feeling value Ai, Ai greater representation of people feel more comfortable. In the period of time [i, j] within the comfort level of people defined as [i, j] is the most uncomfortable feeling that the value of the day * [i, j] the day and feel the value of each. Now give kkk feel value in N consecutive days, may I ask, at what period of time, kkk feel most comfortable?

Input Format

The first row N, the recorded data representing the number of days

The second line of N integers, the representative value feel each day

Output Format

Line, represents the value in the most comfortable feeling for some time in.

Sample input and output

Input # 1
. 6
. 3. 1. 4. 5. 6 2
Output # 1
60

————————————————————————————————————————————

Looking for a few drab and monotonous queue stack test questions before the eighteenth CSP certification of these two algorithms under review (CCF estimate does not give CSP certification out of this problem.

This problem can easily come to violence $ O (n ^ 2) $ approach may be used, or enumeration tables ST interval after pretreatment prefix and the beginning and end of each section to calculate the weight and taking the maximum value as an answer, but this only 70% of the data.

There is also a complex of constant practice is to consider the feelings of the day is the minimum value of $ Ai $ ask the interval, we need to use two variables represents the left and right margins to $ i $ as a starting point to extend this range until the front is a value strictly less than $ Ai $ is stopped, as the title of each value feelings are positive, we can not difficult to prove the correctness of this approach: in the case of determining the range of the minimum and its position, the larger the positive zone coverage, the greater the resulting answer. Also, since the need to enumerate a minimum interval value for each feeling, it can not be limited by the complexity of the algorithm of this question.

So we can consider the optimization approach utilizing monotone stack, we are only concerned with consideration of a feeling adapted to the minimum value at its first end about a position where the value strictly less than the minimum interval, we can use a rigorous monotonically increasing stack (top of the stack maximum) to maintain this relationship. When a value of $ Ai $ feelings are determined to be the minimum stack and ready, we should start to pop up every value of not less than $ Ai $ from the top of the stack (the position of the value recorded at the same time each value in the stack of records ), and mark $ $ array Rig be ejected position these values ​​is $ I $, $ Ai $ representing a first right side thereof is less than its number. The resulting pop-up stack is the number of $ $ $ I $ Aj left of the first is less than its number, the $ Rig [i] $ $ J $ mark, seen from the monotonic nature of the stack and for $ $ LEF $ rig $ array processing is accurate.

code show as below:

#include <bits/stdc++.h>
#define ll long long
#define MAXN 100007
using namespace std;
struct Point { int id,num; }sta[MAXN]; 
int n,tot; ll ans,sum[MAXN];
int a[MAXN],lef[MAXN],rig[MAXN];
inline int read() {
    int w=0,X=0; char ch=0;
    while (!isdigit(ch)) w|=ch=='-',ch=getchar();
    while (isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X; 
}
int main() {
    n=read();
    for (int i=1;i<=n;i++) {
        a[i]=read(),sum[i]=sum[i-1]+a[i];
        while (tot && sta[tot].num>=a[i]) rig[sta[tot--].id]=i;
        lef[i]=tot?sta[tot].id:0;
        sta[++tot]=(Point){i,a[i]};
    }
    for (int i=1;i<=n;i++) {
        if (!rig[i]) rig[i]=n+1;
        ans=max(ans,a[i]*(sum[rig[i]-1]-sum[lef[i]]));
    }
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhwer/p/12031254.html