ZOJ 十六届浙江省赛H.singing everywhere

大一萌新,多多关照,(ɔˆ ³(ˆ⌣ˆc)
第一次写博客,啥也不会,各位大佬多多包涵

H.Singing Everywhere

思路:一开始比较懵,细心思考发现,若要想减少voice crack ,那么就必须删除峰值(也就是voice crack),但是只会减少1个或者2个或者不变,因为只能删除至多一个点,想到这里就可以敲代码了,减少两个的情况就是有两个相邻的并且相等的峰值,比如样例中1 9 1 9 8 1 0,此时删除两个9之间的1即可,减少一个的情况就是删除一个峰值,并且峰值删除以后他左右两边的不会变成新的峰值,不变的情况就是删除一个峰值它左右两边的数有一个变成新的峰值,等于没删,好了分析完了敲代码(没想到一次就Ac了)

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stack>
#include<set>
#include<vector>
using namespace std;
double a[100005];
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		int location[100005];
		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%lf", &a[i]);
			if (i>=2)
			{
				int j = i - 1;
				if (a[j]>a[j - 1] && a[j] > a[j + 1])
				{
					location[ans++] = j;
					//cout << "#" << location[ans - 1];
				}
			}
		}
		int t = ans;
		for (int i = 0; i < t; i++)
		{
			int tans = t;
			int t1 = t;
			int t2 = t;
			int pre = location[i];
			if (a[pre] == a[pre+2])
			{
				tans -= 2;
				ans = tans;
				//cout << "******\n";
				break;
			}
			if (a[pre - 1 - 1] < a[pre]){}
			else t1--;
			if (a[pre - 1] == a[pre + 1])
			{
				t2--;
			}
			else if (a[pre - 1]>a[pre + 1] && a[pre - 2]>a[pre-1])
			{
				t2--;
			}
			else if (a[pre - 1]<a[pre + 1] && a[pre + 2]>a[pre + 1])
			{
				t2--;
			}
			else{}
			tans = t1 < t2 ? t1 : t2;
			if (ans>tans)ans = tans;
		}
		cout << ans << "\n";
	}
	return 0;
}

发布了10 篇原创文章 · 获赞 6 · 访问量 1805

猜你喜欢

转载自blog.csdn.net/Kris_Kris/article/details/89643569