N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的

Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output
6
5

 # include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int sum , num;
int a[] ;
bool v[] ;
int len , n; bool cmp(const int &x , const int &y)
{
return x > y ;
} bool dfs(int count , int L , int pos) //已完成的数量 当前木棒长度 位置
{
if (len == sum)
return ;
if (count == num)
return ;
for (int i = pos ; i < n ; i++)
{
if (v[i])
continue ;
if (L + a[i] == len)
{
v[i] = ;
if (dfs(count+ , , ))
return ;
v[i] = ;
return ;
}
else if (L + a[i] < len)
{
v[i] = ;
if (dfs(count , L+a[i] , i+))
return ;
v[i] = ;
if (L == ) //说明有一根木棒没有派上用场
return ;
while (a[i] == a[i+]) //如果下一根的长度和这根一样 则继续搜索下面的
i++ ;
}
}
return ;
} int main()
{
//freopen("in.txt","r",stdin) ;
while (scanf("%d" , &n) , n)
{
sum = ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d" , &a[i]) ;
sum += a[i] ;
}
sort(a,a+n,cmp) ;
if (a[] > sum - a[]) //如过第1根木棒比剩下的木棒和 还要长
{
printf("%d\n" , sum) ;
continue ;
}
for (len = a[] ; len <= sum ; len++) //一根完整木棒的长度肯定大于最长的小木棒
{
if (sum % len)
continue ;
memset(v , , sizeof(v)) ;
num = sum/len ;
if (dfs(,,))
{
printf("%d\n" , len) ;
break ;
}
}
}
return ; }

hdu 1518 N根木棒 能否拼成正方形  

Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

Sample Output
yes
no
yes

 # include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int sum ;
int a[] ;
bool v[] ;
int len , n; bool cmp(const int &x , const int &y)
{
return x > y ;
} bool dfs(int count , int L , int pos) //已完成的数量 当前木棒长度 位置
{ if (count == )
return ;
for (int i = pos ; i < n ; i++)
{
if (v[i])
continue ;
if (L + a[i] == len)
{
v[i] = ;
if (dfs(count+ , , ))
return ;
v[i] = ;
return ;
}
else if (L + a[i] < len)
{
v[i] = ;
if (dfs(count , L+a[i] , i+))
return ;
v[i] = ;
if (L == ) //说明有一根木棒没有派上用场
return ;
while (a[i] == a[i+]) //如果下一根的长度和这根一样 则继续搜索下面的
i++ ;
}
}
return ;
} int main()
{
// freopen("in.txt","r"a,stdin) ;
int T ;
scanf("%d" , &T) ;
while (T--)
{
scanf("%d" , &n) ;
sum = ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d" , &a[i]) ;
sum += a[i] ;
}
if (sum%)
{
printf("no\n") ;
continue ;
}
sort(a,a+n,cmp) ;
len = sum / ;
if (a[] > len)
{
printf("no\n") ;
continue ;
}
memset(v,,sizeof(v)) ;
if (dfs(,,))
{
printf("yes\n") ;
}
else
printf("no\n") ; }
return ; }

hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)的更多相关文章

  1. hdu 1455 Sticks

    Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

  4. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

  5. hdu 1455(DFS+好题+经典)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...

  7. Sticks HDU - 1455 (未完成)

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  8. HDU 1455 Sticks(经典剪枝)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. Hdu 1455

    #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...

随机推荐

  1. JAVA记录-redis缓存机制介绍(三)

    Redis 事务 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的 ...

  2. css颜色模式hsla和rgba

    在CSS3中可以使用RGBA和HSLA两种色彩模式,这两个都可以用来设置颜色以及指定透明度. rgba指的是:红色.绿色.蓝色.Alpha透明度(Red-Green-Blue-Alpha)前三个值取值 ...

  3. C# Dictionary序列化/反序列化

    [Serializable] public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue& ...

  4. STM32Cube自学-1

    为了方便调试,使用Keil5+Proteus.1.新建Proteus STM32项目,选择STMF103C6 2.新建STM32CubeMX项目,选择同型号CPU,选择UART1,Asynchrono ...

  5. 数据库之MySQL ERROR 1698 (28000) 错误:Access denied for user 'root'@'localhost'" error【摘抄】

    声明:全文均摘抄于MySQL ERROR 1698 (28000) 错误 //错误起源: ~$ mysql -u root -p Enter password: ERROR 1698 (28000): ...

  6. Java EE之Struts2-2.5配置

    开学以来,已经三周了.Java EE却不太走心,于是,这几日空杯心态,重新学习.复习了Java SE和Java Web开发技术,然后入手Struts2.为了使用最新版本的Structs2,我去官网下载 ...

  7. 关于cc -o命令

    这个命令很灵活,格式是: cc -o 目标二进制可执行文件 文件1 文件2 文件3 ..... 其中目标文件后面的文件,可为源代码,也可为二进制文件,也可为库文件 比如: //a.c #include ...

  8. 课程4:黑马程序员_spring2.5视频教程--视频列表

    \黑马程序员_spring2.5视频教程\01Struts相关基础理论介绍.mp4; \黑马程序员_spring2.5视频教程\02搭建struts开发环境.mp4; \黑马程序员_spring2.5 ...

  9. DSO windowed optimization 公式

    这里有一个细节,我想了很久才想明白,DSO 中的 residual 联系了两个关键帧之间的相对位姿,但是最终需要优化帧的绝对位姿,中间的导数怎么转换?这里使用的是李群.李代数中的Adjoint. 参考 ...

  10. 在使用NSArray打印的时候如果遇到中文字符那么会打印出来编码。

    在使用NSArray打印的时候如果遇到中文字符那么会打印出来编码,如下代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any addition ...