poj 1011 Sticks
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 126238 | Accepted: 29477 |
Description
Input
Output
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
uva上面提交超时,poj上可以过;
AC代码:
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 1e4+;
int len[N],sum,L,T;
int used[N];
//bool cmp(int x,int y)
//{
// if(x>y) return true;
//}
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
bool DFS(int m,int left)//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度
{
if(m == && left == )
return true;
if(left == )//一根刚刚拼完
left = L;
for(int i=; i<T; i++)
{
if(!used[i] && len[i]<=left)
{
if(i>)//如果前者已经出现过的不能用,则当前的也不能用
{
if(!used[i-] && len[i] == len[i-])
continue;
}
used[i] = ;
if(DFS(m-,left-len[i]))
return true;
else
{
used[i] = ;
if(len[i] == left || left == L)
return false;
}
}
}
return false;
}
int main()
{
while(scanf("%d",&T) && T)
{ sum = ;
for(int i=;i<T;i++)
{
scanf("%d",&len[i]);
sum = sum + len[i];
}
//sort(len,len+T,cmp); sort 超时
qsort(len,T,sizeof(int),cmp); //从大到小排序
for(L = len[];L<=sum/;L++)
{
if(sum%L) continue;
memset(used,,sizeof(used));
if(DFS(T,L))
{
printf("%d\n",L);
break;
}
}
if(L>sum/) printf("%d\n",sum);
}
return ;
}
poj 1011 Sticks的更多相关文章
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- POJ 1011 Sticks 【DFS 剪枝】
题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- POJ 1011 Sticks(dfs+剪枝)
http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...
- POJ 1011 sticks 搜索
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 125918 Accepted: 29372 Descrip ...
随机推荐
- Android ——Handler相关
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- hdu1003 dp(最大子段和)
题意:给出一列数,求其中的最大子段和以及该子段的开头和结尾位置. 因为刚学过DP没几天,所以还会这题,我开了一个 dp[100002][2],其中 dp[i][0] 记录以 i 为结尾的最大子段的和, ...
- 越狱Season 1-Episode 6: Riots, Drills and the Devil: Part 1
Season 1, Episode 6: Riots, Drills and the Devil: Part 1 - Diamond: Just a few more rides. 就再多玩几次吧 O ...
- JS 的线程、事件循环、任务队列简介
JS 是单线程的,但是却能执行异步任务,这主要是因为 JS 中存在事件循环(Event Loop)和任务队列(Task Queue). 事件循环:JS 会创建一个类似于 while (true) 的循 ...
- Understanding postgresql.conf : log*
After loooong pause, adding next (well, second) post to the “series“. This time, I'd like to describ ...
- poj 1597 Uniform Generator【生成指定范围内所有随机数】
本文参考资料:http://hi.baidu.com/bnjyjncwbdbjnzr/item/1f997cfdd225d5d143c36a58 题意:一个生成随机数的函数, Seed[x+1] = ...
- sql查询重复记录并取对应最小值
原表(aa): id a b 1 22 456 2 22 256 ...
- HBase(六): HBase体系结构剖析(上)
HBase隶属于hadoop生态系统,它参考了谷歌的BigTable建模,实现的编程语言为 Java, 建立在hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它仅能通过主键( ...
- SQL 查询本月无数据用上个月的数据问题
SQL 查询本月无数据用上个月的数据 前言 因为标题有长度限制,先简要说明一下应用场景.比如我们要查一段时间范围内(2013-08-01至2013-12-31)每个月每个运营商的用户总量,每个运营商用 ...
- rsync配置中的auth error,一个隐秘的错误
我们都知道rsync是linux自带的功能强大的文件同步协议. 为了做免密码文件同步,所以,有多种配置.但是种种配置中,可能会遇到一个让人很纠结的问题,那就是安全认证错误的问题! 这个配置,是基于rs ...