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 7Sample Output
1
0
2Hint
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)