USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4445 Accepted Submission(s): 1469
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
题意:T组样例,n条边,这些边任意组合,求能构成多少种三角形
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4445 Accepted Submission(s): 1469
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
题意:T组样例,n条边,这些边任意组合,求能构成多少种三角形
解法,暴力DFS求取有多少种组合方法,但是需要剪枝。就假定a<=b<=c,可以少去很多种情况。可以理解为abc三个数字可以根据排列组合出现几次,避免出现重复的情况,就可以强制要求a<=b<=c。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long LL;
set<LL> fangfangfang;
int n;
int L[20];
void dfs(int a,int b,int c,int cnt)
{
if(cnt == n){
if(a <= b && b <= c && a+b > c){
fangfangfang.insert((LL)a*100000000+(LL)b*10000+c);
}
return;
}
dfs(a+L[cnt],b,c,cnt+1);
dfs(a,b+L[cnt],c,cnt+1);
dfs(a,b,c+L[cnt],cnt+1);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
fangfangfang.clear();
scanf("%d",&n);
for(int i = 0;i < n;i++)
scanf("%d",&L[i]);
dfs(0,0,0,0);
printf("%d\n",fangfangfang.size());
}
return 0;
}