Singing Everywhere ZOJ - 4107

本文介绍了编程竞赛中的一道题目,玩家Baobao在唱歌游戏中尽量减少声音破裂来提高得分。题目要求计算最多删除一个音符后,Baobao能将声音破裂次数降到最低。通过分析输入的歌曲序列,可以找到最优策略来减少破裂次数。文章给出了样例输入和输出,并提供了可能的解决方案思路。
摘要由CSDN通过智能技术生成

https://vjudge.net/problem/ZOJ-4107/origin

https://vjudge.net/contest/395635#problem/H

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 $a_1, a_2,\dots,a_n$, where $a_i$ indicates the $i$-th note in the song. We say a note $a_k$ is a "voice crack" if $1 < k < n$, $a_{k-1} < a_k$ and $a_{k+1} < a_k$. 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 \leq n \leq 10^{5}$), indicating the length of the song.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-2^{31} \leq a_i < 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).

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;


ll a[100005] = {0};
int main()
{
    int t = 0;
    cin >> t;
    while(t--)
    {
        int n = 0;
        cin >> n;
        for(int i = 0;i<n;i++)
        {
            cin >> a[i];
        }
        int mark = 0;
        int counter = 0;
        for(int i = 1;i<n-1;i++)
        {
            if(a[i]>a[i-1]&&a[i]>a[i+1])
            {
                counter += 1;
                if(     a[i-1] <= a[i-2] && a[i-1] <= a[i+1] && a[i+1] <= a[i+2]
                    ||  a[i+1] <= a[i+2] && a[i+1] <= a[i-1] && a[i-1] <= a[i-2])
                {
                    mark = 1;
                }
                if(a[i+2]>a[i+1] && a[i+2]>a[i+3] && a[i]==a[i+2])
                {
                    mark = 2;
                }
            }
        }
        cout << counter - mark << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值