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

hdu4277 USACO ORZ的更多相关文章

  1. HDU4277 USACO ORZ(dfs+set)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  2. hdu 4277 USACO ORZ dfs+hash

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  3. hdu 4277 USACO ORZ DFS

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. HDU 4277 USACO ORZ(暴力+双向枚举)

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. HDU 4277 USACO ORZ(DFS暴搜+set去重)

    原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...

  6. hdu 4277 USACO ORZ

    没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...

  7. hdu 4277 USACO ORZ(dfs+剪枝)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  8. hdu 4277 USACO ORZ (dfs暴搜+hash)

    题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...

  9. hdu 4277 USACO ORZ (Dfs)

    题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...

随机推荐

  1. js 上一天、下一天

    function Previousday() { var curDate = new Date(CreateDate_Temp); * * * ); var strDate = preDate.get ...

  2. AngularJS之使用控制器封装业务逻辑

    AngularJS之使用控制器封装业务逻辑 控制器的作用 我们知道,在AngularJS中,实现数据绑定的核心是scope对象.那么控制器又有什么用呢? 简单地说,没有控制器/controller,我 ...

  3. EasyUi TreeGrid封装

    礼物一:树型实体的抽象与封装 所谓树型实体,就是具有树型结构关系的实体,比如省.市.区.对于初学者,可能会创建三张表进行存储,有经验的开发者通过引入ParentId将设计简化为一张表,但是基于Pare ...

  4. 如何为你的初创应用App开发公司建立战略计划(商业战略竞争五力学)

    首先,什么是战略计划?战略计划可以定义为一个为了达到目标而需要执行的一系列动作步骤的计划. 根据当今全球第一战略权威,商业管理界公认的"竞争战略之父"Michael Porter著 ...

  5. 过度补脑系列:Nokia的不归路

    没有数据,没有调查,只是突然想起诺基亚... 23号的一节酱油课,脑海中突然闪过两个关键字--nokia & android 于是用手机百度了一下,竟然真的出了新机型,NokiaX,X东24日 ...

  6. postal邮件发送(二):Email headers,附件,图片介绍

    接上篇 http://www.cnblogs.com/mybky/p/5690567.html 1.邮件headers 除此之外,还有Reply-To,用于回复邮箱 2.邮件带有图片 邮件发送图片,p ...

  7. 【转】android webview设置内容的字体大小

    Enum for specifying the text size. SMALLEST is 50% SMALLER is 75% NORMAL is 100% LARGER is 150% LARG ...

  8. 10 Super Useful Tools for Web Designers

    36个扁平化设计的iphone和ipad应用界面设计案例 http://designwoop.com/2014/04/36-examples-of-flat-iphone-and-ipad-appli ...

  9. 12个很少被人知道的CSS事实

    之前没有认真的研究过,padding-bottom的值如果是百分比,那么它的实际值是根据父类的宽度来调整的.我还以为是根据这个元素的本身的宽度来定义呢?汗..padding-top/padding-l ...

  10. ps入门教程:选择工具、移动工具、索套工具的使用

    本节课程主要内容:1.学习矩形选择工具.椭圆选择工具.移动工具.多边形套索工具.套索工具.磁性套索工具和魔术 棒选择工具.2.用套索和磁性套索,实现对人物照片的抠图.----------------- ...