D2. Great Vova Wall (Version 2)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 aa of nn integers, with aiai being the height of the ii-th part of the wall.
Vova can only use 2×12×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 ii the current height of part ii is the same as for part i+1i+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 11 of the wall or to the right of part nn 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 nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of parts in the wall.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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
input
Copy
5 2 1 1 2 5
output
Copy
YES
input
Copy
3 4 5 3
output
Copy
NO
input
Copy
2 10 10
output
Copy
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][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][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.
建议先看这一个题:Codeforces Round #527 (Div. 3)D1. Great Vova Wall (Version 1) 好题
题意:题意:一个墙有n个部分,每部分的高度为 ai,现在有 2*1 的砖块去填这个墙,使墙的高度都相同。这题和上一个不同的是:这题的砖头不能垂直放
分析:其实这题和上一题差不多,这一题只有两块相邻的砖头高度相等的时候才能消去,因为砖头不能垂直放了。
砖头不能垂直放了还有的缺陷就是 如果出现一个递增的肯定是不行的(除非消去了, 1 2不行, 但 1 1 2可以)
所以,我们需要保证加入栈的元素不比它的栈头元素大
且最后只剩一个元素的时候,这个元素必须是最大元素
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 100+5
#define rep(i,n) for(int i=0;i<n;i++)
#define sd(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pd(n) scanf("%d\n",n)
#define pll(n) scanf("%lld\n",n)
#define MAX 26
typedef long long ll;
const ll mod=1e6;
ll n,m;
ll a;
ll b[N];
stack<int> s;
int main()
{
scanf("%lld",&n);
ll maxx=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a);
maxx=max(a,maxx);
if(s.empty())
{
s.push(a);
}
else if(a==s.top())
{
s.pop();
}
else if(a>s.top())
{
printf("NO\n");
return 0;
}
else
s.push(a);
}
if((s.size()==0)||(s.size()==1&&s.top()==maxx))
printf("YES\n");
else
printf("NO\n");
return 0;
}