---Restore content begins---
topic address
https://www.luogu.org/problemnew/show/P1970
Topic description
The gardener planted a row of flowers, each with its own height. The flowers grew bigger and more crowded. Dongdong decides
Remove some of the flowers in this row, and leave the rest in place, so that the remaining flowers have room to grow. At the same time, Dongdongxi
Hope the rest of the flowers are arranged in a more unique arrangement.
Specifically, the height of the flowers can be regarded as a list of integers h1, h2..hn. Assuming that after some flowers are removed, the heights of the remaining flowers are g1, g2..gm in order, then Dongdong hopes that at least one of the following two conditions is satisfied:
Condition A: For all g(2i)>g(2i-1), g(2i)>g(2i+1)
Condition B: For all g(2i)<g(2i-1), g(2i)<g(2i+1)
Note that the above two conditions are satisfied at the same time when m = 1, and at most one can be satisfied when m > 1.
Excuse me, how many flowers can Dongdong keep in place at most?
Input and output format
Input format:
The input file is flower .in.
The first line of input contains an integer n representing the number of plants to flower at the beginning.
The second line contains n integers, followed by h1, h2..hn, which represent the height of each flower.
Output format:
The output file is flower.out.
Output a line containing an integer m representing the maximum number of flowers that can stay in place.
Input and output example
5 3 4 2 4 6 2 1 4
For example, in the above example, according to the meaning of the question, it must be arranged according to the top and bottom, then there are two situations: the first and the current, we may wish to discuss them separately (in fact, the idea is the same).
Let's take the downward number as an example. We first put 5 into an array f, indicating that 5 is to be reserved, and then we want to find a smaller number, which happens to be 3 is smaller than 5, so we also put 3 into the array, continue
We're going to find a bigger number, just 4 is bigger than 3, put it in, then a smaller number, put 2 in, then a bigger number, put 4 in (coincidentally.. ), and then we're looking for a smaller number number, but
6 is larger than 4, and then we consider that we can find more numbers by using a larger number as a standard to find a smaller number, so we update 4 with 6, that is, remove 4 (this is the same as the most
where long ascending subsequences are similar). Of course, if the number found is the same as the current number, then follow the second method.
Then we find the upward number in the same way, and take the larger one of the two lengths. Finally the answer (seems simpler than the longest ascending subsequence...),
If the specific code is implemented, my idea is to find a variable to record the direction, and then change the direction every time a new point is found (the so-called direction is actually whether the next point is larger or smaller), and use an array f
To store the number (the number left), and use a variable len to record its length, and finally output the length.
code
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 int n,a[ 100010 ],f[ 100010 ]; 5 bool fx= 0 ; // look for smaller when 0, look for more when 1 big 6 int main() 7 { 8 scanf( " %d " ,& n); 9 for ( int i= 1 ;i<=n;++ i) 10 { 11 scanf( " %d ",&a[i]); // Use a to store the height of the flower 12 } 13 int len1= 0 ; // len is used to record the number of flowers left 14 f[++len1]=a[ 1 ]; / / put the first into 15 first fx= 0 ; // look for the smaller 16 first for ( int i= 2 ;i<=n;++ i) 17 { 18 if (fx== 0 ) 19 { 20 if (a[i]<f[len1]) // If it meets the requirements, put 21 { 22 f[++len1]= a[i]; 23 fx= 1 ; // change the direction 24 } 25 else // update the current point if it does not meet the requirements (without changing the direction) 26 f[len1]= a[i]; 27 } 28 if (fx== 1 ) // The same way to judge the situation of looking up 29 { 30 if (a[i]> f[len1]) 31 { 32 f[++len1]= a[i]; 33 fx = 0 ; 34 } 35 else 36 f[len1]= a[i]; 37 } 38 } 39 memset(f, 0 , sizeof (f)); 40 int len2= 0 ; // Same as above, but to find a larger 41 f[++len2]=a[ 1 ]; 42 fx= 1 ; 43 for ( int i= 2 ;i<=n;++ i) 44 { 45 if (fx== 0 ) 46 { 47 if(f[len2]>a[i]) 48 { 49 f[++len2]=a[i]; 50 fx=1; 51 } 52 else 53 f[len2]=a[i]; 54 } 55 if(fx==1) 56 { 57 if(f[len2]<a[i]) 58 { 59 f[++len2]=a[i]; 60 fx=0; 61 } 62 else 63 f[len2]= a[i]; 64 } 65 } 66 printf( " %d " ,max(len1,len2)); // print the larger of the two cases 67 return 0 ; 68 }