Link:http://acm.hdu.edu.cn/showproblem.php?pid=4277
USACO ORZ
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3798 Accepted Submission(s): 1255
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
AC code:
/*set第一种写法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
struct node{
int a;
int b;
bool operator < (const node &tr)const
{
if(a!=tr.a)
return a<tr.a;
return b<tr.b;
}
}tri;
set<node>se;
int seg[22];
int n,e1,e2,e3,mi;
void dfs(int a,int b,int c,int num)
{
if(num==n)
{
e1=min(a,min(b,c));
e3=max(a,max(b,c));
if(e1==a)
e2=min(b,c);
else if(e1==b)
e2=min(a,c);
else
e2=min(a,b);
if(e1+e2>e3&&e3-e1<e2)
{
tri.a=e1;
tri.b=e2;
if(se.find(tri)!=se.end())
return;
se.insert(tri);
}
return;
}
dfs(a+seg[num+1],b,c,num+1);
dfs(a,b+seg[num+1],c,num+1);
dfs(a,b,c+seg[num+1],num+1);
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
se.clear();
for(i=1;i<=n;i++)
scanf("%d",&seg[i]);
dfs(seg[1],0,0,1);//注意:dfs尽量从第一层开始,不能从第0层,也就是不能dfs(0,0,0,0),这样TLE!!!
printf("%d\n",se.size());
}
return 0;
}*/
/*set第二种写法*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
set<pair<int,int> >se;
set<pair<int,int> >::iterator it;
int seg[22];
int n,e1,e2,e3,mi;
void dfs(int a,int b,int c,int num)
{
if(num==n)
{
e1=min(a,min(b,c));
e3=max(a,max(b,c));
if(e1==a)
e2=min(b,c);
else if(e1==b)
e2=min(a,c);
else
e2=min(a,b);
if(e1+e2>e3&&e3-e1<e2)
{
if(se.find(make_pair(e1,e2))!=se.end())
return;
se.insert(make_pair(e1,e2));
}
return;
}
dfs(a+seg[num+1],b,c,num+1);
dfs(a,b+seg[num+1],c,num+1);
dfs(a,b,c+seg[num+1],num+1);
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
se.clear();
int sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&seg[i]);
sum+=seg[i];
}
dfs(seg[1],0,0,1);//注意:dfs尽量从第一层开始,不能从第0层,也就是不能dfs(0,0,0,0),这样TLE!!!
printf("%d\n",se.size());
/*for(it=se.begin();it!=se.end();it++)
{
printf("%d %d %d\n",it->first,it->second,sum-(it->first+it->second));
}*/
}
return 0;
}