Singing Everywhere(ZOJ-4107)

Problem Description

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 a1,a2,...,an, where ai indicates the i-th note in the song. We say a note ak is a "voice crack" if 1<k<n, ak-1<ak and ak+1<ak. 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 T (about 100), indicating the number of test cases. For each test case:

The first line contains one integer n (1<=n<=10^5), indicating the length of the song.

The second line contains n integers a1,a2,...,an (-2^31<=ai<=2^31), indicating the song performed by BaoBao.

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

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).

题意:t 组数据,每组给出 n 个数,在这 n 个数中,设 1<k<n,若满足 ak-1<ak 并且 ak+1<ak 则 k 视为一个峰,现在可以在这 n 个数中删除 0 或 1 个数,且要使删除后峰的个数最小,求删除后峰的个数

思路:首先根据要求找出序列中的所有山峰,然后考虑单峰、双峰的情况进行模拟即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

int a[N];
int pos[N];
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        if(n==1 || n==2 || n==3)
            printf("0\n");
        else {
            int cnt=0;
            for(int i=2; i<=n-1; i++) //寻找峰
                if(a[i-1]<a[i]&&a[i]>a[i+1])
                    ++cnt;


            if(cnt==0)
                printf("0\n");
            else {
                int maxx=0;
                if(a[1]<a[2]&&a[2]>a[3])//2为峰顶时
                    maxx=1;
                if(a[n-2]<a[n-1]&&a[n-1]>a[n])//n-1为峰顶时
                    maxx=1;

                for(int i=2; i<=n-1; i++) {
                    if(a[i-1]<a[i]&&a[i]>a[i+1]) { //i为峰顶时
                        if(a[i-1]!=a[i+1]) {
                            if(i>=3)
                                if(a[i-1]>a[i+1] && a[i-2]>=a[i-1])
                                    maxx=max(maxx,1);
                            if(i>=n-2)
                                if(a[i-1]<a[i+1] && a[i+1]<=a[i+2] )
                                    maxx=max(maxx,1);
                        }
                    }
                    else if(i>=3&&i<=n-2) {
                        if(a[i-2]<a[i-1] && a[i-1]>a[i] && a[i]<a[i+1] & a[i+1]>a[i+2] ) { //双峰时
                            if(a[i-1]==a[i+1])
                                maxx=2;
                            else
                                maxx=max(maxx,1);
                        }
                    }
                }
                printf("%d\n",cnt-maxx);
            }
        }
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值