hdu4277 USACO ORZ(dfs+set判重)

USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2278    Accepted Submission(s): 814


Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments. 
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.
 

Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)
 

Output
For each test case, output one integer indicating the number of different pastures.
 

Sample Input
  
  
1 3 2 3 4
 

Sample Output
  
  
1
 

Source

题目大意:给n个木棍,求能组成不同三角形个数。要求全部用完,全等算一个,相似算不同的。

题目分析:比赛的时候男神做的。Orzns。。。

木棍不超过15根,爆搜之

对于第i个木棍,有3种方法,所以总的搜索次数是3^15。加点剪枝还是能过的。

搜的时候最好按顺序搜,方便剪枝,规定搜的3条边a<=b<=c,那么当a>sum/3就可以不搜了。

另外判重也很重要,比赛的时候男神是用的二维map判的重,其实用个set判重就可以了,将三角形3条边看成一个sum进制数,sum是木棍总长度,然后放进set就可以了。答案就是set的大小。

详情请见代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
int lcm[20];
int n,sum;
bool flag[20];
set<__int64>mse;

void deal(int a,int b)
{
    int c = sum - a - b;
    if(c - a >= b || a + b <= c)
        return ;
    //printf("%d %d %d\n",a,b,c);
    __int64 tmp = sum * sum * a + sum * b + c;
    mse.insert(tmp);
}

void dfs(int a,int b,int c,int dp)
{
    if(a > sum / 3)
        return;
    if(dp == n + 1)
    {
        if(a > b || b > c || !a || !b || !c)
            return;
        deal(a,b);
        return;
    }
    dfs(a + lcm[dp],b,c,dp + 1);
    dfs(a,b + lcm[dp],c,dp + 1);
    dfs(a,b,c + lcm[dp],dp + 1);
}

int main()
{
    int i,t;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d",&n);
        for(sum = 0,i = 1;i <= n;i ++)
        {
            scanf("%d",&lcm[i]);
            sum += lcm[i];
        }
        mse.clear();
        memset(flag,false,sizeof(flag));
        dfs(0,0,0,1);
        printf("%d\n",mse.size());
    }
    return 0;
}
//953MS	5860K
/*
60
3
2 3 4
5
1 1 1 1 1
5
1 2 3 4 5
9
1 1 1 1 1 1 1 1 1
10
1 1 1 1 1 1 1 1 1 1
11
1 1 1 1 1 1 1 1 1 1 1
12
1 1 1 1 1 1 1 1 1 1 1 1
13
1 1 1 1 1 1 1 1 1 1 1 1 1
14
1 1 1 1 1 1 1 1 1 1 1 1 1 1
15
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

*/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值