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
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.
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)
3
2 3 4
#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的更多相关文章
- HDU4277 USACO ORZ(dfs+set)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- hdu 4277 USACO ORZ dfs+hash
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 4277 USACO ORZ DFS
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 4277 USACO ORZ(暴力+双向枚举)
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- hdu 4277 USACO ORZ
没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- hdu 4277 USACO ORZ(dfs+剪枝)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- hdu 4277 USACO ORZ (dfs暴搜+hash)
题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...
- hdu 4277 USACO ORZ (Dfs)
题意: 给你n个数,要你用光所有数字组成一个三角形,问能组成多少种不同的三角形 时间分析: 3^15左右 #include<stdio.h> #include<set> usi ...
随机推荐
- jquery 仅仅读
大家都理解这是什么,正常的写法例如以下: if (status == true) { $("#minDelistStr").val(totalAmount);// 去掉首部的&qu ...
- centos 7安装源
参照 http://www.linuxidc.com/Linux/2015-03/114690.htm http://www.cnblogs.com/mchina/archive/2013/01/04 ...
- HDU Redraw Beautiful Drawings 推断最大流是否唯一解
点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 ...
- 将已有的工程项目添加到Xcode到Git管理中
在Xcode中创建工程的时候,我们很容易的可以将新创建的工程添加到Git中,如图: 但是如果是本地已经有的工程,那该如何添加到Git中呢? 首先终端进入到该工程的目录. 然后: git init gi ...
- Ejb in action(七)——message与JMS
我们扩大MDBs学前,我们需要理解message(新闻)与JMS(Java Message Service)的概念. 我们在Java EE中谈论消息,实际上就是意味着实现一个松耦合的过程.系统组件之间 ...
- POI导出Excel文档通用工具方法
import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; imp ...
- WPF 读写TxT文件
原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...
- OSG(OpenSceneGraphcow.osg)配置笔记
OpenSceneGraph是一款高性能的3D图形开发库.广泛应用在可视化仿真.游戏.虚拟现实.高端技术研发以及建模等领域.使用标准的C++和OpenGL编写而成,可以运行在Windows系列.OSX ...
- __declspec(novtable)keyword
__declspec (novtable )keyword,表示这个类不生成虚函数表.可是继承类不影响(无论基类是否使用了keyword). 不使用此keyword.类在生成对象时构造函数和析构函数多 ...
- iOS文档序列化(对象归档)
对象归档: 概念: 对象归档是指将对象写入文件保存在硬盘,当再次又一次打开程序时,能够还原这些对象.也称:对象序列化.对象持久化. 数据持久性的方式(事实上就是3类) 1,NSKeyedArchive ...