您的位置:首页 > 其它

HDU 4277 USACO ORZ (dfs + hash)

2013-05-21 19:48 295 查看
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;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: