More than 2019 cattle off summer school training 1

Equivalent Prefixes

This is a topic with monotonous stack, as to why you can use monotonous stack?

The two arrays simultaneously run monotonous stack, if every time a push at most, of course, before this push will certainly be a number of pop,

If there are a number into the stack, and then judge for themselves this time into the stack of the number of positions it is not the same, if not identical to explain is definitely wrong.

Why at this time, only the position within the stack it?

First, the minimum number before this is certainly not considered, because the current bottom of the stack is the smallest of the position, function and value after the number before the number is certainly RMQ function returns the bottom of the stack.

Then that is considered within the stack elements, this should be a very good thought.

#include <cstdio> // description "If found, say" X, let's fly "(Here, X is the start time) ..." 
#include <cstdlib> // and Output in the "X, let's fly" 
#include <CString> // in the " '" and "'" is not a symbol, do not copy wrong! ! ! 
#include <Queue> 
#include <Vector> 
#include <algorithm> 
#include < String > 
#include <Stack> 
#include <the iostream> 
#include <Map>
 #define INF 0x3f3f3f3f
 #define inf64 0x3f3f3f3f3f3f3f3f
 the using  namespace STD;
 long long ll;
int queue_mina[maxn];
int queue_minb[maxn];
int a[maxn], b[maxn];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        bool flag = 1;
        int ans = n;
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
        int f1 = 1, t1 = 0;
        int f2 = 1, t2 = 0;
        int r = 1;
        while(r<=n)
        {
            while (f1 <= t1 && a[r] < a[queue_mina[t1]]) t1--;
            queue_mina[++t1] = r;
            while (f2 <= t2 && b[r] < b[queue_minb[t2]]) t2--;
            queue_minb[++t2] = r;
            //printf("t1=%d t2=%d queue_mina[%d]=%d queue_minb[%d]=%d\n", t1, t2, t1, queue_mina[t1], t2, queue_minb[t2]);
            if (t1 != t2)
            {
                flag = 0;
                ans = r - 1;
                break;
            }
            r++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
A

 

Guess you like

Origin www.cnblogs.com/EchoZQN/p/11246775.html