Singing Everywhere 模拟
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 ***** (the 4th note), and no matter which note he deletes, he will also always sing 1 voice *****.

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

题目大意是说,对给定的数列删掉一个数或者不删,问最少的数字山峰有几个,数字山峰即左右两个数都比中间的数小。

需要区分一下各种情况,不论删除与否,最多都只能使数字山峰减少两个,即左右两个数都比中间值小且两个数相等,这种情况下选择删除中间的数,可以获得最小值。如果无法使删除两个的情况成立,就找减少一个的,需要比较删除后是否会出现新的山峰,即比较当前位置的左右两个,注意不要越界。另外,边界位置不考虑,因为边界位置不能满足左右两个都小于中间值的限制。

AC代码

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cmath>
#include<queue>
using namespace std;
int main()
{
	int t,n,i;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int a[100005],peak[10005],cnt=0,book[100005]={0};
		for(i=0;i<n;i++)
			cin>>a[i];
		for(i=1;i<n-1;i++)
			if(a[i]>a[i-1]&&a[i]>a[i+1])
			{
				peak[cnt++]=i;
				book[i]=1;
			}
		int flag=0;
		for(i=1;i<n-1;i++)
		{
			if(book[i-1]&&book[i+1])
			{
				if(a[i-1]==a[i+1])
				{
					flag=2;
					break;
				}
				else 
					flag=1;
			}
			if(book[i])
			{
				int t=i-1;
				int t2=i+1;
				if(t-1>=0&&t+2<n&&a[t]>a[t+2]&&a[t]>a[t-1]||t2+1<n&&t2-2>=0&&a[t2]>a[t2+1]&&a[t2]>a[t2-2]);
				else flag=1;
			}
		}
		cout<<cnt-flag<<endl;
	}
	return 0;
} 

相关文章:

  • 2021-04-04
  • 2021-09-06
  • 2021-09-06
  • 2021-08-11
  • 2022-01-07
  • 2021-08-22
  • 2022-12-23
猜你喜欢
  • 2021-10-16
  • 2021-08-28
  • 2022-12-23
  • 2021-07-30
  • 2021-12-02
  • 2021-12-02
  • 2021-09-12
相关资源
相似解决方案