USACO ORZ(DFS+set去重)

181 篇文章 0 订阅
171 篇文章 0 订阅


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.
 

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
 

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值