思路:直接搜索

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int belong[5010],num[5010],n;
int dfs(int s,int pre,int cur)
{
    int i,j;
    if(cur==n/2)
        return 1;
    for(i=s;i<=n;i++)
    {
        if(belong[i]==-1)
        {
            belong[i]=0;
            for(j=i+1;j<=n;j++)
            if(belong[j]==-1&&num[i]==num[j]&&j>pre)
            {
                belong[j]=1;
                if(dfs(s+1,j,cur+1))
                    return 1;
                belong[j]=-1;
            }
            belong[i]=-1;
            break;
        }
    }
    return 0;
}
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(belong,-1,sizeof(belong));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d",num+i);
        dfs(1,0,0);
        for(i=1;i<=n;i++)
            printf("%d",belong[i]);
        printf("\n");
    }
    return 0;
}

 

相关文章:

  • 2021-07-10
  • 2021-10-12
  • 2021-11-02
  • 2021-12-07
  • 2021-06-26
  • 2022-02-14
  • 2021-07-22
  • 2021-09-25
猜你喜欢
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
相关资源
相似解决方案