题意:
一串数字,将其分为两个等长子序列,且同一个序列内元素顺序不变,要求两个子序列一致的一种可行方案。其中必定有解,且每个数字出现次数为2次或4次。
题解:
比赛时以为有什么贪心的方法,结果都被自己的生成的数据cha了,赛后用搜索1A,简单粗暴快……
首先强制任何时候1序列的长度大于等于2序列长度。
如果一个数字只出现两次,那肯定第一个分给序列1,第二个分给序列2.
如果一个数字出现4次,那第二个可以分给1序列也可以分给序列2,而其余3个分到哪个序列都是确定的了。
这样就行了……
给两组样例,其实可以自己写个生成器对拍,很容易的:
6
3 3 2 3 3 2
6
2 3 2 2 3 2
//Time:15ms
//Memory:356KB
//Length:1402B
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <set>
#define MAXN 6010
#define INF 1000000007
#define MP(x,y) make_pair(x,y)
#define FI first
#define SE second
#define EPS 1e-8
using namespace std;
int cnt[MAXN],num[MAXN],sta[MAXN],n;
bool vi[MAXN];
char ans[MAXN];
bool dfs(int h,int he,int ta)
{
for(;h<n;++h)
if(ta==he||num[h]!=sta[he])
sta[ta++]=num[h],ans[h]='0';
else if(vi[h]==1)
{
ans[h]='1';
if(dfs(h+1,he+1,ta)) return true;
sta[ta++]=num[h];
ans[h]='0';
if(dfs(h+1,he,ta+1)) return true;
}
else ++he,ans[h]='1';
if(h==n&&he==ta) return true;
return false;
}
int main()
{
//freopen("/home/moor/Code/input","r",stdin);
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d",&n);
memset(cnt,0,sizeof(cnt));
memset(vi,0,sizeof(vi));
for(int i=0;i<n;++i)
scanf("%d",&num[i]);
for(int i=n-1;i>=0;--i)
{
++cnt[num[i]];
if(cnt[num[i]]==3)
vi[i]=1;
}
dfs(0,0,0);
ans[n]='\0';
printf("%s\n",ans);
}
return 0;
}