hdu 4272 2012长春赛区网络赛 dfs暴力 ***

时间:2023-11-19 22:33:38

总是T,以为要剪枝,后来发现加个map就行了

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
bool vis[MAXN];
int a[MAXN];
bool dfs(int pos)
{
while(vis[pos]==&&pos>) pos--;
if(pos==) return ;
if(pos==)
{
return ;
}
int temp=pos-;
for(int i=;i<=;i++)
{
if(temp<=) return ;
if(vis[temp])
{
i--;
temp--;
continue;
}
if(a[pos]==a[temp]) //找到
{
vis[temp]=;
if(dfs(pos-)) return ;
vis[temp]=;
}
temp--;
}
return ;
}
int main()
{
int i,j,k,ca=;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
map<int,int> mp;
for(i=;i<=n;i++)
{
scanf("%d",a+i);
mp[a[i]]++;
}
if(n%)
{
printf("0\n");
continue;
}
bool f=;
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
if((it->second)%==)
{
f=;
break;
}
}
if(!f)
{
printf("0\n");
continue;
}
cl(vis);
printf("%d\n",dfs(n));
}
}