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 ...
随机推荐
- 通俗易懂地解决中文乱码问题(2) --- 分析解决Mysql插入移动端表情符报错 ‘incorrect string value: '\xF0...
原文:[原创]通俗易懂地解决中文乱码问题(2) --- 分析解决Mysql插入移动端表情符报错 'incorrect string value: '\xF0... 这篇blog重点在解决问题,如果你对 ...
- 《自己动手写CPU》写书评获赠书活动结果
<自己动手写CPU>写书评获赠图书的读者有: 京东:8***2.16号哨兵.magicyu.kk6803.jddickyd.杰出的胡兵 亚马逊:徐贺.马先童.jaychen.farmfar ...
- Touch Punch在移动设备上面增加jQuery UI的触摸支持|Jquery UI 支持移动端 触摸滑动等
jQuery UI是我们前台开发常用的UI前端类库,但是目前的jQuery UI用户界面类库在互动和widget上并不支持touch事件.这意味着你在桌面上设计的优雅的UI可能在触摸设备,例如,ipa ...
- CSS知识总结之浏览器(持续更新)
web页面浏览器渲染过程 1.解析html文件,并构建DOM树: 在DOM树中,每一个html标签都有一个对应的节点,并且每一个文本也有一个对应 的节点(js的textNode),DOM树的根节点就是 ...
- PCIe固态存储和HDD常见的硬盘性能对比测试
2周测试后,导致以下结果 MySQL-OLTP测试结果:(50表.每个表1000广域网数据,1000个线程) TPS:MySQL在PCIe固态存储上执行是在HDD上执行的5.63倍 writes:My ...
- 对web.config的ConnectionString加密
原文:对web.config的ConnectionString加密 本文参考了wayshan的博客,原文地址:http://www.cnblogs.com/wayshan/archive/2012/0 ...
- C# 我的注册表操作类
using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...
- JTAG应该如何接线
下面是某个ARM9评估板的原理图: 注意: 1. Vref和Vtarget可以直接连在一起,由被调试板提供3.3V或5V电源: 2. nTRST,最好上拉: 3. TDI,最好上拉 4. TMS,最好 ...
- 如果你使用上述这段12行的JavaScript代码,就可以能让firefox、chrome、safari浏览器崩溃,而且还能让iphone重启,安卓手机闪退!
<html> <body> <script> var total=""; for (var i=0;i<1000000;i ) { tot ...
- 2014由于在myeclipse5.5.1许可证
点击假设Myeclipse负载项目server该图标不响应.这是MyEclipse过期,这也是一年许可: subscriber: axin Serial:nLR8ZC-855575-69517252 ...