H - Pillars

There are nn pillars aligned in a row and numbered from 11 to nn.

Initially each pillar contains exactly one disk. The ii-th pillar contains a disk having radius a_iai​.

You can move these disks from one pillar to another. You can take a disk from pillar ii and place it on top of pillar jj if all these conditions are met:

  1. there is no other pillar between pillars ii and jj. Formally, it means that |i - j| = 1∣i−j∣=1;
  2. pillar ii contains exactly one disk;
  3. either pillar jj contains no disks, or the topmost disk on pillar jj has radius strictly greater than the radius of the disk you move.

When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.

You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all nn disks on the same pillar simultaneously?

Input

The first line contains one integer nn (3 \le n \le 2 \cdot 10^53≤n≤2⋅105) — the number of pillars.

The second line contains nn integers a_1a1​, a_2a2​, ..., a_iai​ (1 \le a_i \le n1≤ai​≤n), where a_iai​ is the radius of the disk initially placed on the ii-th pillar. All numbers a_iai​ are distinct.

Output

Print YES if it is possible to place all the disks on the same pillar simultaneously, and NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

Examples

Input

4
1 3 4 2

Output

YES

Input

3
3 1 2

Output

NO

题意解读:本题类似于课本上学习递归汉诺塔;但比它简单好多。

本题先输入一个N表示有N个柱子,下面一行输入N个数据表示第i个柱子上的盘子大小,要求最终全在一个柱子上并且大盘在下小盘在上

一次挪动一个柱子上的盘子,并且大盘不能放在小盘子之上。so,本题突破点来了,我们需要在录入数据的时候找到最大盘所在的柱子。

下面开始精华部分,因为大盘不能在小盘子上面,所以我们最开始找到的最大盘子所在的柱子就是我们最终所有盘子都要在这个柱子上

**********************************************************

**大盘不能在小盘子上**所以我们每次只能从大盘子左边一个与右边一个中挑选一个最大的盘子,但是不能大过我们需要压的盘子的大小,否则就是这组数据不符合题意,挪动后更新最大值,挪动坐标

*********************************************************

代码如下:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const long long maxn=2*1e5+5;
int a[maxn];
int main() {
	int N;
	cin>>N;
	int max=0,k=0;
	for(int i=1; i<=N; i++) {
		cin>>a[i];
		if(a[i]>max) {
			max=a[i];
			k=i;
		}
	}//找到最大盘子以及最大盘子所在坐标 
	//下面赋值是为了防止数组越界使用随机值
	a[0]=a[N+1]=0;
	int l=k-1,r=k+1;
	int flag=1;
	while(1) {
		if(l<=0&&r>=N+1)
			break;//此时遍历完毕 ,完成放盘子
		if(a[l]>a[k]||a[r]>a[k]) { //必须从最大旁边的两个挑盘子,也就是说,除了最大盘,第二大盘必须在最大盘两侧,否则无法移动
			flag=0;
			break;
		}
		if(a[l]>a[r]) {
			k=l;//最大盘子坐标更换 
			l--;
		} else {
			k=r;//同上 
			r++;
		}
	}
	if(flag==1)
		cout<<"YES"<<endl;
	else
		cout<<"NO"<<endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值