USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2309 Accepted Submission(s): 826
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.
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)
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
这个题就不多说了,我太菜了。。。。。
题意:用完所有的篱笆,组成三个边,问这样的的三角形(不同)一共有多少个
dfs+set判重,每一段篱笆可以去a,b或者c边,我们假设a<=b<=c,dfs深搜下去,等到所有的篱笆搜索完了,判重之后的size就是题目要求的数目
//这个是纯暴力的,跑了900多毫秒,有的人写的跑了200多毫秒,差距好大。。。
#include<stdio.h>
#include<iostream>
#include<set>
using namespace std;
set<__int64> s;
int len[20],n;
__int64 sum;
void dfs(int a,int b,int c,int num)
{
__int64 tem;
if(num==n+1)
{
if(a>b||b>c||a>c)//这一步可以减少下面的运算过程
return ;
if(a&&b&&c&&a+b>c)
{
tem=(a*sum+b)*sum+c;
s.insert(tem);
}
return ;
}
dfs(a+len[num],b,c,num+1);
dfs(a,b+len[num],c,num+1);
dfs(a,b,c+len[num],num+1);
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
s.clear();
scanf("%d",len+i);
sum+=len[i];
}
dfs(0,0,0,1);
printf("%d\n",s.size());
}
return 0;
}