思路:直接搜索
#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(" "); } return 0; }