More than 2019 cattle off summer school camp (first) A - Equivalent Prefixes (monotonous stack)

The meaning of problems


Given an array of n-$ $ $ two elements a, b $, their front array $ p $ elements constituting the "equivalent", find the maximum value of $ p $. "Equivalent" means the same as a minimum value in any subinterval thereof. $ [Link] $

analysis


This question has two approaches, Cartesian tree and monotonous stack, where only introduced for the time being monotonous stack approach.

Let's assume that $ p = i $ founded, to consider new add to the mix of $ i + 1 $, all sections if $ i + 1 $ is right dots of the same minimum, then $ p $ can be updated to $ i + $ 1 (so it can be demonstrated by sequentially minimum position between the large cells of the same position of the same minimum section). Or in other words, $ 1 $ I + affect the value of the front section at a minimum due to the same, considered $ p = i + 1 $ established.

Consider using monotonous stack to do (because it is the minimum that we maintain a monotonically increasing stack). Every monotonous stack elements will answer to the top element to the end of the minimum of the interval is.

Such as arrays:
2,4,3,5,1

Monotone before a stack elements:
{2,1} {}
means [1,1] is the minimum value of 2

Monotone stack former two elements:
{{2,1}, {4,2}}
means the minimum value of [1,2] is 2, [2,2] is the minimum value of 4

Monotone stack first three elements:
{{2,1}, {3,3}}
means [1,3] is the minimum value of 2, [2,3] is the minimum value of 3, [3,3] the minimum is 3

Monotone stack first four elements:
{{2,1}, {3,3}, {5,4}}
means [1,4] is the minimum value of 2, [2,4] is minimum 3 , [3,4] is the minimum value of 3, [4,4] is minimum 5

Monotone stack first five elements:
{5,1} {}
means [x, 5] is a minimum value of 1

Two "equivalent" of the number of elements in the array monotonous sure the same stack, we put the value of $ i + 1 $ where when there will be added to the list or into the stack after stack operation out there, so every time need only compare monotonous stack size you can know the impact of $ i + 1 $ result is the same, namely a new element is added to make the array to maintain "equivalent."

Code

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+5;
int a[maxn], b[maxn], sta[maxn], stb[maxn];
int n, topa, topb;

int main()
{
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
        int ans = 0;
        topa = topb = 0;
        for(int i = 1; i <= n; i++) {
            while(topa&&a[sta[topa]]>=a[i]) topa--;
            sta[++topa] = i;
            while(topb&&b[stb[topb]]>=b[i]) topb--;
            stb[++topb] = i;
            if(topa==topb) ans++;
            else break;
        }
        printf("%d\n", years); 
    } 
    
}
View Code

 


Reference blog:

https://www.cnblogs.com/Yinku/p/11210511.html

https://blog.csdn.net/qq_41289920/article/details/96899277

https://www.cnblogs.com/kongbursi-2292702937/p/11280900.html

 

Guess you like

Origin www.cnblogs.com/wizarderror/p/11600624.html