hdu4277 USACO ORZ

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.

 
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
没想到就用暴搜就可以了,当时还以为要用什么状态压缩啊,什么的,还有,这题,说是要把所有的边都要用到,而不是,有的边可以不用,这点,错了一次,下次一定要审好题啊,还有,这里用set来判重,是个很好的方法!
#include <iostream>
#include <set>
#include <stdio.h>
using namespace std;
set<__int64> myset;
int bian[3];
int num[10005],sum[10005],n,a,b,c;
int dfs(int step)
{
int i,temp;
a=bian[0],b=bian[1],c=bian[2];
if(step==n+1)
{
if(a<=b&&b<=c&&(a+b)>c)
{
//printf(" %d %d %d\n",a,b,c);
myset.insert(a*100000000000000+b*1000000+c);
}
return -1;
} temp=sum[n]-sum[step-1];
if(b+temp<=a)
{
return -1;
}
if(c+temp<=b)
{
return -1;
}
if(c+temp<=a)//
{
return -1;
}
if(a+b+temp<=c)
return -1; for(i=0;i<3;i++)
{
bian[i]+=num[step];
dfs(step+1);
bian[i]-=num[step];
}
return -1;
}
int main()
{
int tcase ,i;
scanf("%d",&tcase);
while(tcase--)
{
myset.clear();
scanf("%d",&n);
sum[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
sum[i]=num[i]+sum[i-1]; }
dfs(1);
printf("%d\n",myset.size());
}
return 0;
}

再来一个hash函数的

#include <iostream>

#include <string.h>
#include <stdio.h>
using namespace std; #define maxprime 1000007
int bian[3],re;
__int64 hash[maxprime];
int num[20],n,a,b,c;
__int64 sum[20]; bool hashjudge(__int64 val)
{
int v;
v=val%maxprime;
while(hash[v]!=-1&&hash[v]!=val)
{
v+=20;
v=v%maxprime;
}
if(hash[v]==-1)
{
hash[v]=val ;
re++;
return true;
}
return false ;//是重复访问返回假
}
int dfs(int step)
{
int i,temp;
a=bian[0],b=bian[1],c=bian[2];
if(step==n+1)
{
if(a<=b&&b<=c&&(a+b)>c)
{
//printf(" %d %d %d\n",a,b,c);
// myset.insert();
__int64 t=a*sum[n]*sum[n]+b*sum[n]+c;
hashjudge(t); }
return -1;
} temp=sum[n]-sum[step-1];
if(b+temp<=a)
{
return -1;
}
if(c+temp<=b)
{
return -1;
}
if(c+temp<=a)
{
return -1;
}
if(a+b+temp<=c)
return -1; for(i=0;i<3;i++)
{
bian[i]+=num[step];
dfs(step+1);
bian[i]-=num[step];
}
return -1;
}
int main()
{
int tcase ,i;
scanf("%d",&tcase);
while(tcase--)
{
//myset.clear();
memset(hash,-1,sizeof(hash));
scanf("%d",&n);
sum[0]=0;
re=0;
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
sum[i]=num[i]+sum[i-1]; }
dfs(1);
printf("%d\n",re);
}
return 0;
}

上一篇:GD库常用函数


下一篇:lockf