zoukankan      html  css  js  c++  java
  • 思维题+栈的应用——cf1092D有意思

    第一例很简单,把两个差为偶数的列不断合并即可

    这种不需要撤销的合并相连数直接用栈来做

    /*
    如果相邻两列高度差为偶数
    那么可以直接消去 
    */
    #include<bits/stdc++.h>
    #include<stack>
    using namespace std;
    #define maxn 200005
    stack<int>stk;
    int n,a[maxn];
    int main(){
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=n;i++){
            if(stk.empty()){
                stk.push(a[i]);
            }
            else {
                if(abs(stk.top()-a[i])%2==0){
                    stk.pop();
                }
                else stk.push(a[i]);
            }
        }
        if(stk.size()<2)puts("YES");
        else puts("NO");
    }

    第二例:只能横填

    那么只要从最低的开始往上填,即不断将偶数个低列和其周围的列合并

    用单调栈完美解决!

    #include<bits/stdc++.h>
    #include<stack>
    using namespace std;
    #define maxn 400005
    int n,a[maxn],top;
    struct Node{
        int h,w;
    }stk[maxn];
    int main(){
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=n;i++){
            if(top==0){
                stk[++top].h=a[i];
                stk[top].w=1;
            }
            else{
                if(stk[top].h==a[i])
                    stk[top].w++;
                else if(stk[top].h<a[i]){
                    while(top && stk[top].h<a[i]){
                        if(stk[top].w%2==0){
                            stk[top-1].w+=stk[top].w;
                            top--;
                        }
                        else {
                            puts("NO");
                            return 0;
                        }
                    }
                    if(stk[top].h==a[i])
                        stk[top].w++;
                    else {
                        stk[++top].h=a[i];
                        stk[top].w=1;
                    }
                }
                else if(stk[top].h>a[i]){
                    stk[++top].h=a[i];
                    stk[top].w=1;
                }
            }
        }
        if(top!=1){
            for(int i=2;i<=top;i++)
                if(stk[i].w%2){
                    puts("NO");
                    return 0;
                }
        
        }
        puts("YES");
    }
  • 相关阅读:
    ubuntu12.04下设置IPv6
    ubuntu下vncserver配置
    ubuntu12.04安装gerrit代码审核服务器
    Linux + Eclipse 配置交叉编译环境
    进程间通信基础
    ubuntu下搭建git服务器
    udev
    rpcgen的简单讲解及例子程序
    rpc简介、原理、实例-缘于difx
    windows下常用开源软件
  • 原文地址:https://www.cnblogs.com/zsben991126/p/10914685.html
Copyright © 2011-2022 走看看