Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9414    Accepted Submission(s): 2776

Problem Description
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
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
 
Sample Output
6
5
 

题意:给多组数据,每组数据代表一些小木棍,能否将它们全部用完组成(尽量)多个相同长度的长棍,并输出长棍的长度。

题解:有强剪枝,,多一个if(l==0) return false就AC了。。真的是NB啊,这处剪枝的作用:当len==0时,证明当前是在构造一根新的木棍,而这个第一根和其后面的所有的木棍都无法组合,那么这根木棍永远都无法用到了,所以我们直接返回上一层。下面那个while循环的剪枝作用就是当前木棍无法用到,那么其后面的所有的木棍都无法用到了,跳过即可。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
using namespace std; int n,sum,num;
int v[];
bool vis[];
int cmp(int a,int b){
return a>b;
}
bool dfs(int len,int l,int cnt,int pos){
if(cnt==num) return true;
for(int i=pos;i<n;i++){
if(vis[i]) continue;
if(len==l+v[i]){
vis[i] = true;
if(dfs(len,,cnt+,)) return true;
vis[i] = false;
       return false; ///加了这个才能过poj,这个代表如果当前长度的木棍以后的木棍都拼不出这个长度了,那么就没必要试这个长度了,(后面都比当前小...)直接跳出..ORZ
}else if(len>l+v[i]){
vis[i] = true;
if(dfs(len,l+v[i],cnt,i+)) return true;
vis[i] = false;
if(l==) return false;
while(v[i]==v[i+])i++;
}
}
return false;
}
int main(){
while(scanf("%d",&n)!=EOF,n){
sum = ;
for(int i=;i<n;i++){
scanf("%d",&v[i]);
sum+=v[i];
}
sort(v,v+n,cmp);
int ans = sum;
for(int i=v[];i<=sum;i++){
if(sum%i!=) continue;
num = sum/i;
memset(vis,false,sizeof(vis));
if(dfs(i,,,)) {
ans = i;
break;
}
}
printf("%d\n",ans);
}
}

hdu 1455(DFS+好题+经典)的更多相关文章

  1. [HDU]1016 DFS入门题

    题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...

  2. hdu 1455 Sticks

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

  3. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  4. POJ 1321 棋盘问题(DFS板子题,简单搜索练习)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44012   Accepted: 21375 Descriptio ...

  5. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  6. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  7. Educational Codeforces Round 12补题 经典题 再次爆零

    发生了好多事情 再加上昨晚教育场的爆零 ..真的烦 题目链接 A题经典题 这个题我一开始推公式wa 其实一看到数据范围 就算遍历也OK 存在的问题进制错误 .. 思路不清晰 两个线段有交叉 并不是端点 ...

  8. 咸鱼的ACM之路:DFS水题集

    DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...

  9. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. 史上最全Linux提权后获取敏感信息方法

    http://www.freebuf.com/articles/system/23993.html 在本文开始之前,我想指出我不是专家.据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享( ...

  2. RabbitMQ 作用

    1.RabbitMQ 作用 同步变异步 解耦 削峰 2.

  3. 51Nod 1092 回文字符串 | 最长公共子序列变形

    求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...

  4. 51Nod 1002 数塔取数问题

    Input示例 4 5 8 4 3 6 9 7 2 9 5 Output示例 28 DP: 递推式: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+arr[i][j]; ...

  5. vijos 1057 盖房子 简单DP

    描述 永恒の灵魂最近得到了面积为n*m的一大块土地(高兴ING^_^),他想在这块土地上建造一所房子,这个房子必须是正方形的. 但是,这块土地并非十全十美,上面有很多不平坦的地方(也可以叫瑕疵).这些 ...

  6. Sass 基本特性-运算 感觉满满都是坑

    Sass中的基本运算 一.加法 在 CSS 中能做运算的,到目前为止仅有 calc() 函数可行.但在 Sass 中,运算只是其基本特性之一.      sass做加法运算是可以不考虑参数带单位,但需 ...

  7. js刷新页面方法 -- (转)

    1,reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet])   参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里 ...

  8. Runas replacement tool

    1. RunAsSpc Runas 无法在脚本中输入密码,可以使用RunAsSpc替代. RunAsSpc = runas + password + encryption https://robotr ...

  9. 网络协议之HTTP协议

    HTTP协议详解(真的很经典) 转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HT ...

  10. Mojo_1_第一个简单例子

    use Mojolicious::Lite; #根目录,Get方法打开 #正接显示文本text get '/' => sub{ my $service = shift; $service-> ...