HDU 4277 USACO ORZ (dfs + hash)

USACO ORZ

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


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

/*
  dfs + hash
  <1>首先是三角形的性质(sum为周长):
  1)最短边小于等于sum/3
  2)最长边小于等于sum/2
  3)两边之和大于第三边,两边之差小于第三边
  <2>dfs的时候,可以固定某条边为最短边,枚举每条栏杆,如果最短边仍小于sum/3,
  把栏杆加到最短边上。如果最短边已经大于sum/3,此时它已经不是最短边,那么把
  当前边加到另外一条边上。搜到低的时候判断是否满足三角形性质
  <3>hash的时候用一个稍大的质数*某条边+另外一条边,因为两条边确定就可以确定
  第三条边了。
  最后,用set去重。
  */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define hash 10007

using namespace std;

int T,N,sum;
int fen[16];
set <int> st;

void dfs(int pos,int len1,int len2,int len3)
{
    if(pos == N+1)
    {
        if(len1 <= len2 && len2 <= len3 && len1 + len2 > len3)
            st.insert(hash*len1+len2);
        return;
    }
    if(len1 + fen[pos] <= sum/3)dfs(pos+1,len1+fen[pos],len2,len3);
    if(len2 + fen[pos] <= sum/2)dfs(pos+1,len1,len2+fen[pos],len3);
    if(len3 + fen[pos] <= sum/2)dfs(pos+1,len1,len2,len3+fen[pos]);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        sum = 0;
        scanf("%d",&N);
        for(int i=1; i<=N; i++)
        {
            scanf("%d",&fen[i]);
            sum += fen[i];
        }
        st.clear();
        dfs(1,0,0,0);
        printf("%d\n",st.size());
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值