Vova’s family is building the Great Vova Wall (named by Vova himself). Vova’s parents, grandparents, grand-grandparents contributed to it. Now it’s totally up to Vova to put the finishing touches.
The current state of the wall can be respresented by a sequence a of n integers, with ai being the height of the i-th part of the wall.
Vova can only use 2×1 bricks to put in the wall (he has infinite supply of them, however).
Vova can put bricks only horizontally on the neighbouring parts of the wall of equal height. It means that if for some i the current height of part i is the same as for part i+1, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can’t put bricks in such a way that its parts turn out to be off the borders (to the left of part 1 of the wall or to the right of part n of it).
Note that Vova can’t put bricks vertically.
Vova is a perfectionist, so he considers the wall completed when:
all parts of the wall has the same height;
the wall has no empty spaces inside it.
Can Vova complete the wall using any amount of bricks (possibly zero)?
Input
The first line contains a single integer n (1≤n≤2⋅105) — the number of parts in the wall.
The second line contains n integers a1,a2,…,an (1≤ai≤109) — the initial heights of the parts of the wall.
Output
Print “YES” if Vova can complete the wall using any amount of bricks (possibly zero).
Print “NO” otherwise.
Examples
inputCopy
5
2 1 1 2 5
outputCopy
YES
inputCopy
3
4 5 3
outputCopy
NO
inputCopy
2
10 10
outputCopy
YES
Note
In the first example Vova can put a brick on parts 2 and 3 to make the wall [2,2,2,2,5] and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it [5,5,5,5,5].
In the second example Vova can put no bricks in the wall.
In the third example the wall is already complete.
题意:
给你一堵墙从头到尾的高度,你有1*2的砖头,你只能横着放,就是相邻两块同样高的墙砖高度都+1,问你最后能不能让这堵墙的高度都一样高。
题解:
这题是上题稍微变化一下,遇到两个相同的就消掉,但是要弄一个最大值的数组,就是维护消掉两边的最大值,比如说 1 2 3 3 2 1,那么消掉的只能是3,因为3大于2所以并不能在旁边放墙砖,3 3 2 1 1 2,那么全都可以消掉,最大值的变化为: 3 3 0 0 0 0 ->3 3 0 1 1 0->3 3 2 1 1 2.
#include<bits/stdc++.h>
using namespace std;
#define pa pair<int,int>
stack<pa>s;
const int N=2e5+5;
int maxn[N];
int main()
{
int n;
scanf("%d",&n);
int x,flag=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(s.size()&&s.top().first==x&&x>=maxn[i-1]&&s.top().first>=maxn[s.top().second])
maxn[i]=max(maxn[i-1],x),maxn[s.top().second]=max(maxn[s.top().second+1],x),s.pop(),flag=x;
else
s.push({x,i});
}
if(s.size()==0)
printf("YES\n");
else if(s.size()==1&&n%2==1)
{
int m=0;
for(int i=1;i<=n;i++)
m=max(maxn[i],m);
if(s.top().first>=m)
printf("YES\n");
else
printf("NO\n");
}
else
printf("NO\n");
return 0;
}