The 15th ZJP ACM - Pro.A Peak

本文介绍了一种算法,用于判断给定整数序列是否符合峰值定义:即存在唯一一个索引 k,使得序列左侧严格递增,右侧严格递减,且 k 不位于序列首尾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A sequence of integers a1,a2,,ana1,a2,…,an is called a peak, if and only if there exists exactly one integer kk such that 1<k<n1<k<n, and a1<a2a1<a2 for all ,ai1>aiai−1>aiand for all k<ink<i≤n .

Given an integer sequence, please tell us if it’s a peak or not. 
Input

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer n(3n105)n(3≤n≤105), indicating the length of the sequence.

The second line contains integers a1,a2,,ana1,a2,…,an (1ai2×1091≤ai≤2×109), indicating the integer sequence.

It’s guaranteed that the sum of in all test cases won’t exceed .

Output

For each test case output one line. If the given integer sequence is a peak, output “Yes” (without quotes), otherwise output “No” (without quotes).

Sample Input



1 5 7 3 2 

1 2 1 2 1 

1 2 3 4 

4 3 2 1 

1 2 1 

2 1 2 

1 2 3 1 2 
Sample Output

Yes 
No 
No 
No 
Yes 
No 
No

就这找到一个最大值,最大值的左边递减,最大值的右边也递减,最大值不能为开头或者是结尾..看代码

# include <stdio.h>
# include <string.h>

int main(void)
{
	int t, n, i, m, max, d, j, e, e1;
	int a[100001];
	scanf("%d", &t);
	while (t --)
	{
		max = 0;
		scanf("%d", &n);
		for (i = 1; i <= n ; i++)
		{
			scanf("%d", &a[i]);
			if (a[i] > max)
			{
				max = a[i];
				d = i;
			}
		}
		if (d == 1 || d == n)
			printf("No\n");//开头结尾为0,结束
		else
		{
			e = 0;
			for (j = 1; j < d; j ++)
			{
				if (a[j] >= a[j+1])//  记得要有等号
				{
					e = 1;
					break;
				}
			}//从前往最大值比较
			if (e == 1)
				printf("No\n");
			else
			{
				e1 = 0;
				for (j = d; j <= n-1; j ++)
				{
					if (a[j] <= a[j+1])// 记得等号
					{
						e1 = 1;
						break;
					}
				}// 从后往前比较
				if (e1 == 1)
					printf("No\n");
				else
					printf("Yes\n");
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值