博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
I - 剪枝 0。O HDU - 1455 Sticks DFS
阅读量:7093 次
发布时间:2019-06-28

本文共 1914 字,大约阅读时间需要 6 分钟。

 

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero. 

Input

The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero. 

Output

The output file contains the smallest possible length of original sticks, one per line. 

Sample Input

95 2 1 5 2 1 5 2 141 2 3 40

Sample Output

65

题意:输入n个整数,把这n给整数分成m堆,使得这m堆每一堆的和都相等,求最小的和

思路:因为数据很小,所有可以直接枚举和,搜索是否可行,考点在剪枝

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long LL;using namespace std;const int maxn=111;int n;int suma=0,maxa=0;int a[maxn],vis[maxn];bool cmp(int a,int b){ return a>b;}bool DFS(int len,int need_len,int x)//和为len,还需need_len,还有x个数字没用{ if(x==0) { printf("%d\n",len); return true; } if(need_len==0)//准备构成新的len need_len=len; for(int i=0;i
=a[i]) { vis[i]=1; if(DFS(len,need_len-a[i],x-1))return true;//把a[i]放到当前堆里 vis[i]=0; if(a[i]==need_len||need_len==len)return false;/*剪枝 到这里是放入a[i]不能构成len 1.如果此时a[i]==need_len,说明放入a[i]可以构成一个新的len,但是没有成功 要继续下去的话,就是将这个a[i]变成其他的几个之和,然后在后面构成的新的len里用a[i], 但因为每个数字都必须且只能使用一次,所以使用a[i]和几个数之和等于a[i]的其实是一样的, 所以如果这样不能成功,当前的路不通 而且这里不用a[i]后面也要用,还不如先用,留下那几个小的数构成后面的更灵活 2.need_len==len,说明正准备进行新一轮的组合,但是新的len却不能构造成功,即即是说加入a[i]的话, 剩下的数不能构造出len,但是a[i]一定要用上,所以可知当前的路不通 */ int j=i; while(i

 

转载于:https://www.cnblogs.com/107acm/p/9428321.html

你可能感兴趣的文章
QTP的那些事--有关一个webtable数据的获取案例
查看>>
【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成
查看>>
EXTJS学习系列提高篇:第十一篇(转载)作者殷良胜,制作树形菜单之五
查看>>
ylbtech-memorandum(备忘录)-数据库设计
查看>>
Oracle数据库服务器CPU持续100%之等待事件asynch descriptor resize
查看>>
java8中的localdate和localtime用法举例
查看>>
8天学通MongoDB——第四天 索引操作
查看>>
linux命令
查看>>
程序员之路
查看>>
MISP2:初始阶段
查看>>
详解:Redis主从技术的应用
查看>>
maven 笔记,具体配置
查看>>
Linux学习笔记<二十二>——计划任务
查看>>
Python3 通过 pika 连接 RabbitMQ 的基本用法
查看>>
C/C++踩坑记录(二)一段有趣的常量字符串
查看>>
GDI+ 学习记录(2): 画笔线帽 - Cap
查看>>
一张表里的多个字段值 取自 字典表里的text 的查询
查看>>
golang tcp socket
查看>>
特么的程序员励志故事(小IT职员在北京5年买了500W的房子)
查看>>
全选和反选 checkbox
查看>>