Hack数据未通过求调
查看原帖
Hack数据未通过求调
378346
expnoi楼主2023/2/2 16:39
#include<bits/stdc++.h>
using namespace std;
int n,a[1010],mi[1010],f[1010][1010],col[1010];
stack<int> st1,st2;
inline bool dfs(int u,int colo)
{
    if(col[u])
    {
        if(col[u]!=colo)return 0;
        return 1;
    }
    col[u]=colo;
    for(int i=1;i<=n;i++)
    {
        if(!f[u][i])continue;
        int v=i,color=0;
        if(colo==1)color=2;
        if(colo==2)color=1;
        if(!dfs(v,color))return 0;
    }
    return 1;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    mi[n+1]=n+1;
    for(int i=n;i>=1;i--)mi[i]=min(mi[i+1],a[i]);
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(a[i]<a[j]&&a[i]>mi[j+1])
                f[i][j]=f[j][i]=1/*,cout<<i<<" "<<j<<"\n"*/;
    for(int i=1;i<=n;i++)
    {
        if(!col[i]&&!dfs(i,1))
        {
            //cout<<i<<"\n";
            puts("0");
            return 0;
        }
    }
    int w=1;
    for(int i=1;i<=n;i++)
    {
        if(col[i]==1)
        {
            while(st1.size()&&st1.top()==w)
            {
                st1.pop();
                cout<<"b ";
                ++w;
            }
            cout<<"a ";
            st1.push(a[i]);
        }
        else
        {
            while(st1.size()&&st1.top()==w)
            {
                st1.pop();
                cout<<"b ";
                ++w;
            }
            while(st2.size()&&st2.top()==w)
            {
                st2.pop();
                cout<<"d ";
                ++w;
            }
            cout<<"c ";
            st2.push(a[i]);
        }
    }
    while(true)
    {
        if(st1.size()&&st1.top()==w)
        {
            cout<<"b ";
            st1.pop();
            ++w;
        }
        else if(st2.size()&&st2.top()==w)
        {
            cout<<"d ";
            st2.pop();
            ++w;
        }
        else break;
    }
}
2023/2/2 16:39
加载中...