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

Portal

Meaning of the questions:

Input a n, which are representative of two arrays of n numbers, then let you to find a p <= n, so as to satisfy (1 <= l <= r <= p <= n) allows the (L , r) in the inner section of two arrays of the same minimum index

 

answer:

Reference blog:

I always thought that the interval starting point has been a 1 l, suddenly found that he could also become T_T

p is 1 sure against blanket

When p is greater than 1, we only need to determine the location of the first minimum of two arrays each position on the left met are the same, has not found the same location, that location is the biggest p, then this then the same can be demonstrated by sequentially minimum position between the position of the large cells of the same minimum section

Suppose we prove (4) the minimum range position

Prerequisite (3,4) in the same position of minimum 3, (3) the minimum position

solution:

Then the (3,4) position of the minimum is found in this range is 3, and the same as the minimum position of the two sections (1,3), so the whole (4) section are the same as the minimum position

 

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<stack>
 8 using namespace std;
 9 const int maxn=1e5+10;
10 int a[maxn],b[maxn],la[maxn],lb[maxn];
11 stack<int>sa,sb;
12 int main()
13 {
14     int n;
15     while(~scanf("%d",&n))
16     {
17         for(int i=1; i<=n; ++i)
18         {
19             scanf("%d",&a[i]);
20         }
21         for(int i=1; i<=n; ++i)
22         {
23             scanf("%d",&b[i]);
24         }
25         while(!sa.empty ()) sa.pop ();
 26 is          for ( int I = . 1 ; I <= n-; ++ I)
 27          {
 28              the while ! (sa.empty () && A [I] < A [SA. Top ()]) sa.pop ();
 29              IF (sa.empty ()) La [I] = 0 ;
 30              the else La [I] = sa.top ();    // Get the left is less than the first it number 
31 is              sa.push (I); // if against his left that it is not less than the number that is greater than the number of left or desirable number 
32          }
 33 is          the while (! sb.empty ()) sb.pop ();
 34 is          for ( int I = . 1 ; I <= n-; ++ I)
35         {
36             while(!sb.empty() && b[i]<b[sb.top()]) sb.pop();
37             if(sb.empty()) lb[i]=0;
38             else lb[i]=sb.top();
39             sb.push(i);
40         }
41         int ans=1;
42         for(int i=2;i<=n;++i)
43         {
44             if(la[i]==lb[i]) ans=i;
45             else break;
46         }
47         printf("%d\n",ans);
48     }
49     return 0;
50 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11280900.html