HDU 4665 Unshuffle

15 篇文章 0 订阅
14 篇文章 0 订阅

比赛

题目

题意:

一串数字,将其分为两个等长子序列,且同一个序列内元素顺序不变,要求两个子序列一致的一种可行方案。其中必定有解,且每个数字出现次数为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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值