#include<bits/stdc++.h>
using namespace std;
int a[200007];
stack<int>s;
int main(){
int n;
int mn=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>mn)
mn=a[i];
}
s.push(a[1]);
int x=0;
for(int i=2;i<=n;i++){
if(!s.empty()){
x=s.top();
if(a[i]==x)
s.pop();
else{
if(a[i]>x){
printf("NO");
return 0;
}
s.push(a[i]);
}
}
else
s.push(a[i]);
}
int y=s.size();
if(y<=1){
if(y==1&&s.top()!=mn)//如果栈内剩下的是最大的元素,它无需与其它元素一同++,非最大是无法和其他一起++的,缺少该判断惨遭hack
printf("NO");
else
printf("YES");
}
else
printf("NO");
return 0;
}
//类似括号匹配
相关文章
- Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
- Codeforces Round #527 (Div. 3) C. Prefixes and Suffixes (思维,字符串)
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
- Codeforces Round #527 (Div. 3)
- Codeforces Round #527 (Div. 3)D2(栈,思维)