【CF 1197B】Pillars

                                            B. 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 aiai.

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≤n≤2⋅1053≤n≤2⋅105) — the number of pillars.

The second line contains nn integers a1a1, a2a2, ..., aiai (1≤ai≤n1≤ai≤n), where aiai is the radius of the disk initially placed on the ii-th pillar. All numbers aiai 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

题意:类似于汉诺塔,加了个限制:只能从相邻的两个柱子上转移盘子,小的盘子可以放在大的盘子上,问最终能否将所有盘子按照顺序放在同一根柱子上

因为最终要转移到一个柱子上,所以从最大的盘子开始,将相邻的盘子从大到小依次放到这根柱子上

从中间向两遍进行遍历,最终两端的盘子都能转移则代表成功

如果已经放上的盘子比下一个盘子小,则证明转移不能成功

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))

int a[200005];

int main()
{
	int n,i,maxn=-inf,k=0,flag=1;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		if(maxn<a[i])
		{
			maxn=a[i];
			k=i;
		}
	}
	a[0]=0;
	a[n+1]=0;
	int l=k-1,r=k+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&&l==0&&r==n+1)
		cout<<"YES"<<endl;
	else
		cout<<"NO"<<endl;
	return 0;
}

 

全部评论

相关推荐

黑曼巴在线招人:草拟
点赞 评论 收藏
分享
2025年10月3日中午,在写完定时一年后发给自己的信之后,敲下键盘,写下这篇文字。我把标题的“所有人”加了引号,因为如我们所见,确实有的人顺风顺水,每天过的很开心,或是早早进入大厂,或是年纪轻轻就拿到了高薪offer,或是过着可能我努力十年也不一定实现的生活。但也许,不是每个人的痛苦都能被别人看到的,这个月我经常会哭,被骗6000块钱、手上钱不够导致拖欠房租、生活还要借朋友钱、国庆长假也没有钱去旅游,互联网公司不稳定担心试用期不过(毕竟上段实习就是被裁了,一有点风吹草动就害怕),但这样的我,不是所有人都知道的,居然是有些朋友的羡慕对象。回忆我的七年“长跑”别人都是多年幸福的恋爱长跑,我没有恋...
故事和酒66:让每一颗种子找到合适自己的生长方式,最终绽放出独一无二的花朵,这远比所有人都被迫长成同一棵“参天大树”的世界,更加美好和富有生机。这是社会和环境的问题,而不是我们的问题。然而就是在这样的环境中,楼主依然能突破自我,逆势成长,其中的艰辛可想而知。这一路的苦难终究会化作你成长的养料
你小时候最想从事什么职业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务