求助原始数据
查看原帖
求助原始数据
118296
siyue楼主2022/7/15 10:49
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n;
int a[1005],f[1005],color[1005];
bool g[1005][1005],flag=true;
stack<int> st1,st2;

bool dfs(int x,int y)
{
    color[x]=y;
    int i;
    for(i=1; i<=n; i++)
        if(g[x][i]&&(color[i]==y||(color[i]==-1&&!dfs(i,!y))))
            return false;
    return true;
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    int i,j;
    cin>>n;
    for(i=1; i<=n; i++)
        cin>>a[i];
    f[n+1]=n+1;
    memset(g,false,sizeof g);
    memset(color,-1,sizeof color);
    for(i=n; i>=1; i--)
        f[i]=min(f[i+1],a[i]);
    for(i=1; i<=n; i++)
        for(j=i+1; j<=n; j++)
            if(a[i]<a[j]&&f[j+1]<a[i])
                g[i][j]=g[j][i]=true;
    for(i=1; i<=n; i++)
        if(color[i]==-1&&!dfs(i,0))
        {
            flag=false;
            break;
        }
    if(!flag)
    {
        cout<<0<<endl;
        return 0;
    }
    int now=1;
    for(i=1; i<=n; i++)
    {
        if(color[i]==0)
        {
            while(st1.size()&&st1.top()==now)
            {
                st1.pop();
                cout<<"b ";
                now++;
            }
            st1.push(a[i]);
            cout<<"a ";
        }
        else
        {
            while(1)
            {
                if(st1.size()&&st1.top()==now)
                {
                    st1.pop();
                    cout<<"b ";
                }
                else if(st2.size()&&st2.top()==now)
                {
                    st2.pop();
                    cout<<"d ";
                }
                else
                    break;
                st2.push(a[i]);
                cout<<"c ";
                now++;
            }
        }
    }
    while(1)
    {
        if(st1.size()&&st1.top()==now)
        {
            st1.pop();
            cout<<"b ";
        }
        else if(st2.size()&&st2.top()==now)
        {
            st2.pop();
            cout<<"d ";
        }
        else
            break;
        now++;
    }
    return 0;
}
2022/7/15 10:49
加载中...