Singing Everywhere

版权声明:转载时 别忘了注明出处 https://blog.csdn.net/ZCY19990813/article/details/89639761

Baobao loves singing very much and he really enjoys a game called Singing Everywhere, which allows players to sing and scores the players according to their performance.

Consider the song performed by Baobao as an integer sequence , where  indicates the -th note in the song. We say a note  is a "voice crack" if ,  and . The more voice cracks BaoBao sings, the lower score he gets.

To get a higher score, BaoBao decides to delete at most one note in the song. What's the minimum number of times BaoBao sings a voice crack after this operation?

Input

There are multiple test cases. The first line of the input contains an integer  (about 100), indicating the number of test cases. For each test case:

The first line contains one integer  (), indicating the length of the song.

The second line contains  integers  (), indicating the song performed by BaoBao.

It's guaranteed that at most 5 test cases have .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

3
6
1 1 4 5 1 4
7
1 9 1 9 8 1 0
10
2 1 4 7 4 8 3 6 4 7
Sample Output

1
0
2

Hint

For the first sample test case, BaoBao does not need to delete a note. Because if he deletes no note, he will sing 1 voice crack (the 4th note), and no matter which note he deletes, he will also always sing 1 voice crack.

For the second sample test case, BaoBao can delete the 3rd note, and no voice cracks will be performed. Yay!

For the third sample test case, BaoBao can delete the 4th note, so that only 2 voice cracks will be performed (4 8 3 and 3 6 4).

题意:将山峰定义为一个序列中大于左右两边的数,问删掉序列中的一个数会使得序列中山峰数量最少。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define MAX 0x3f3f3f3f
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int M=1e5+10;
LL a[M];
int main()
{
    //freopen("//home//acm//桌面//in","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n,ans=0;
        scanf("%lld",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
        }
        int flag=0;
        for(int i=2; i<n; i++)
        {
            if(a[i]>a[i-1]&&a[i]>a[i+1])
            {
                ans++;//ans记原数组中有几个山峰,flag是最多会减少几个
                if(a[i+2]>a[i+1]&&a[i+2]>a[i+3]&&i+3<=n){
                    flag=2;
                    if(a[i+2]!=a[i])
                        flag--;
                }
                else {
                    if(a[i+1]<a[i-1]&&a[i-2]<a[i-1]&&i-2>=1)
                        flag=max(flag,0);
                    else
                        flag=max(flag,1);
                }
            }
        }
        printf("%lld\n",ans-flag);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCY19990813/article/details/89639761