2012 ACM/ICPC Asia Regional Changchun Online-1011(hdu4277)

//http://acm.hdu.edu.cn/showproblem.php?pid=4277

//直接搜索,加一点点剪枝;


#include<stdio.h>
#include<map>
using namespace std;
int num[20],sum,n,ant,tem[20];
map <int,int> p[80000];
void dfs(int dep,int a,int b)
{
    int t,c;
    c=ant-(a+b);
    if(dep>n)
    {
        if(a>b){t=a,a=b,b=t;}
        if(a>c){t=a,a=c,c=t;}
        if(b>c){t=b,b=c,c=t;}
        if(a+b>c&&!p[a][b])
        {
            p[a][b]=1;
            sum++;    
        }
    }
    else
    {
        if(a+num[dep]<=ant/2)
            dfs(dep+1,a+num[dep],b);
        if(b+num[dep]<=ant/2)
            dfs(dep+1,a,b+num[dep]);
        if(tem[dep]-a-b<=ant/2)
            dfs(dep+1,a,b);
    }
}
int main()
{
    int cas,i;
    scanf("%d",&cas);
    while(cas--)
    {
        ant=0;
        scanf("%d",&n);
        for(i=1,tem[0]=0;i<=n;i++)
        {
            scanf("%d",&num[i]);
            ant+=num[i];
            tem[i]=ant;
        }
        for(i=1;i<=ant/2+1;i++)
            p[i].clear();
        sum=0;
        dfs(1,0,0);
        printf("%d\n",sum);
    }
    return 0;
}


你可能感兴趣的:(2012 ACM/ICPC Asia Regional Changchun Online-1011(hdu4277))