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 ...
随机推荐
- Hadoop-2.4.1学习之Writable及事实上现
Hadoop基于DataInput和DataOutput实现了简单.高效的序列化协议,而Writable接口定义了Hadoop序列化的方法,MapReduce框架中的不论什么键值类型都实现了该接口,比 ...
- deb包+软件图标+添加到系统菜单+举例安装卸载
本文介绍的内容和实验一下: 1. 制造deb包.2. 为了使软件图标.3. 开始菜单中添加到系统中的软件:4. 安装和卸载制作的deb包. 1. 制作deb包 制作deb包的方法可能有多种,本文使用的 ...
- Bundle压缩JS和CSS
ASP.NET MVC之Bundle压缩JS和CSS 介绍Bundle之前先引用<淘宝技术这十年>中一段话,对Web前端稍微有点常识的人都应该知道,浏览器下一步会加载页面中用到的CSS.J ...
- SQL点滴20—T-SQL中的排名函数
原文:SQL点滴20-T-SQL中的排名函数 提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到ide ...
- C语言链表操作模板(添加,删除,遍历,排序)
C语言链表操作模板,摘自郝斌的C语言视频教程,简单的修改成了纯C格式.当年照着视频学习的时候记录下来的,在使用的时候直接拿来修改修改修改能节约不少时间的. /********************* ...
- leetcode 第42题 Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- js 监控浏览器关闭事件
代码如下: <!DOCTYPE html> <html> <head> <title>监控浏览器关闭事件</title> </head ...
- ADFS 2.0 配置简介 PartⅡ – 配置 ADFS 信任关系
ADFS 与应用程序间的各种验证是基于信任关系的,在 ADFS 服务器配置好要信赖的应用程序(以 URL 为标识)后,应用程序再通过指定认证服务器来将用户引导至 ADFS 登录页,登录完成后再将用户的 ...
- css 初始化
html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,caption,th,td,for ...