Longest Waveform Sequence Length || Gardener

  

3. Gardener

                                       ps This test question can be found in Luogu

Time and space limit 1000ms / 128MB

Topic description

The gardener planted a row of flowers, each with its own height. The flowers grew bigger and more crowded. Dongdong decided to remove some of the flowers in the row and leave the rest in place, so that the remaining flowers could have room to grow. At the same time, Dongdong hoped that the remaining flowers would be arranged in a more unique manner.

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

Input sample #1 : 

5

5 3 2 1 2

Sample output #1 : 

3

illustrate

【Explanation of input and output samples】

There are multiple ways to keep exactly 3 flowers, for example, leave the 1st, 4th, and 5th plants with heights of 5, 1, and 2, which satisfy condition B.

【data range】

For 20% of the data, n ≤ 10;

For 30% of the data, n ≤ 25;

For 70% of the data, n ≤ 1000, 0 ≤ ℎi≤ 1000;

For 100% of the data, 1 ≤ n ≤ 100,000, 0 ≤ hi ≤ 1,000,000, all hi are randomly generated, and all random numbers obey a uniform distribution within a certain interval.




 

This issue is seen at a glance

Find the longest waveform sequence length

Does the name sound familiar?

Yes, the longest ascending subsequence

This question feels similar to him, and even the complexity is lower than his, it seems that only

//I don't know if this method is dp

The whole is divided into two parts

1. Seek 121

2,. Seek 212

Take an example

5 3 2 1 2

 



 

 

#include<cstdio>
#define maxn 100000+10
using namespace std;
int max(int x,int y)
{
    return x>y?x:y;
}
int n;
int a[maxn];
int f[maxn];
int main()
{
    freopen("flower.in","r",stdin);
    freopen("flower.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
    }
    int len ​​= 1 ;
    f[1]=a[1]; 
    for(int i=2;i<=n;i++)
    {
        if(len%2)//奇数  121
        {
            if(f[len]<a[i])
                f [ ++ len] = a [i];
            else 
                f [len] = a [i];
        }
        else
        {
            if(f[len]>a[i])
                f [ ++ len] = a [i];
            else 
                f [len] = a [i];
        }
    }
    int lon=1;
    f[1]=a[1];
    for(int i=2;i<=n;i++)
    {
        if(lon%2)//奇数 212
        {
            if(f[lon]>a[i])
                f[++lon]=a[i];
            else
                f[lon]=a[i];    
        }
        else
        {
            if(f[lon]<a[i])
                f[++lon]=a[i];
            else
                f[lon]=a[i];    
        }
    }
    printf("%d",max(lon,len));
    return 0;
}

 


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324600138&siteId=291194637