Luogu3952 NOIP2017时间复杂度

  搞一个栈模拟即可。对比一下和一年前考场上的代码233

//2018.11.8
#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 110
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int getlim() { char c=getc(); if (c=='n') return 1000; else { int l=c^48,c=getchar(); if (c>='0'&&c<='9') l=l*10+(c^48); return l; } } int T,top; bool isenter[N],isconst[N],isappear[256]; char var[N]; int main() { T=read(); while (T--) { memset(isappear,0,sizeof(isappear));top=0; int n=read();getc(); char c=getc();int p; if (c=='1') p=0;else p=read(); bool iserr=0;int cnt=0,ans=0,isout=0; while (n--) { c=getc(); if (c=='F') { char v=getc(); if (isappear[v]) iserr=1; int l=getlim(),r=getlim(); if (!iserr) { var[++top]=v;isappear[v]=1; isconst[top]=r-l<=100; isenter[top]=r>=l; if (!isout) ans=max(ans,cnt+=isconst[top]^1); isout+=isenter[top]^1; } } else { if (top==0) iserr=1; if (!iserr) { isout-=isenter[top]^1; if (!isout) cnt-=isconst[top]^1; isappear[var[top]]=0; top--; } } } if (top!=0) iserr=1; if (iserr) cout<<"ERR"<<endl; else if (ans==p) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
//2017.11.11
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int t,n,w,top;
bool flag=0,a[256],ifn[1000],ifcmp[1000];
char stk[1000];
int main()
{
    cin>>t;
    while (t>0)
    {
        t--;
        cin>>n;
        char c=getchar();
        while ((c<'0'||c>'9')&&(c!='n')) c=getchar();
        //cout<<c<<endl;
        if (c=='1') w=0;
        else
        {
            c=getchar();
            cin>>w;
        }
        //cout<<w<<endl;
        c=getchar();
        top=0;
        memset(a,0,sizeof(a));
        memset(ifcmp,0,sizeof(ifcmp));
        flag=1;
        int o=0,MAX=0,iflr=0;
        for (int i=1;i<=n;i++)
        {
            c=getchar();while (c!='E'&&c!='F') c=getchar();
            if (c=='E')
            {
                if (top<=0) flag=0;
                else
                {
                    a[stk[top]]=0;
                    if (ifn[top]) o--;
                    if (ifcmp[top]) iflr--;
                    top--;
                }
            }
            else
            {
                char x;int l,r;
                cin>>x;char tmp=x;
                if (a[x]) flag=0;
                else
                {
                    a[x]=1;
                    cin>>x;
                    if (x=='n') l=101;
                    else
                    {
                        l=x-48;
                        x=getchar();
                        if (x>='0'&&x<='9') l=l*10+x-48;
                    }
                    cin>>x;
                    if (x=='n') r=101;
                    else
                    {
                        r=x-48;
                        x=getchar();
                        if (x>='0'&&x<='9') r=r*10+x-48;
                    }
                    if (l>r)
                    {
                        top++;
                        stk[top]=tmp;
                        ifn[top]=0;
                        ifcmp[top]=1;
                        iflr++;
                    }
                    else if(l<101&&r<101||l==r)
                    {
                        top++;
                        stk[top]=tmp;
                        ifn[top]=0;
                        ifcmp[top]=0;
                    }
                    else
                    {
                        top++;
                        stk[top]=tmp;
                        if (iflr) ifn[top]=0;
                        else 
                        {
                            ifn[top]=1;
                            o++;
                            if (o>MAX) MAX=o;
                        }
                        ifcmp[top]=0;
                    }
                }
            }
        }
        if (top>0) flag=0;
        if (!flag) cout<<"ERR"<<endl;
        else
        {
            if (MAX==w) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Gloid/p/9930446.html