码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #527 (Div. 3)D2(栈,思维)

时间:2018-12-21 14:10:29      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:div   top   name   def   bsp   括号   empty   pac   family   

#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)D2(栈,思维)

标签:div   top   name   def   bsp   括号   empty   pac   family   

原文地址:https://www.cnblogs.com/ldudxy/p/10155259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!