HDU1455(DFS)
来源:互联网 发布:表达爱意的网络用语 编辑:程序博客网 时间:2024/06/11 07:30
Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4434 Accepted Submission(s): 1210
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
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65//深度搜索,注意以当前长度、当前已找根数、当前下标为参数,同时注意先排序以降低时间复杂度,#include<iostream>#include<algorithm>using namespace std;int len[70];bool hash[70];int sum,n,av;int m;bool cmp(int a,int b){ return a>b;}bool dfs(int s,int le,int num){ int i; if(num==m) return true; for(i=s+1;i<n;i++) { if(!hash[i]) { if(le+len[i]==av) { hash[i]=true; if(dfs(-1,0,num+1)) return true; hash[i]=false; } else if(le+len[i]<av) { hash[i]=true; if(dfs(i,le+len[i],num)) return true; hash[i]=false; //无此则会超时,原因是会导致大量本可排除的搜索,假如不能凑齐相同长度,则不必进行直接返回。 if(le==0)return false; while(i<n-1&&len[i]==len[i-1]) i++; } } } return false;}int main(){ int i; int low,high,mid; while(scanf("%d",&n)!=EOF&&n) { sum=0; for(i=0;i<n;i++) { hash[i]=false; scanf("%d",&len[i]); sum+=len[i]; } sort(len,len+n,cmp); for(av=len[0];av<=sum;av++) { if(sum%av) continue; m=sum/av; if(dfs(-1,0,1)) break; } printf("%d\n",av); } return 0;}
- HDU1455(DFS)
- HDU1455-经典DFS剪枝
- hdu1455 Sticks ----DFS
- HDU1455 Sticks(DFS+剪枝)
- hdu1455
- hdu1455
- HDU1455
- hdu1455
- hdu1455
- HDU1455
- hdu1455
- ACM-DFS之Sticks——hdu1455
- hdu1455 dfs搜索之凑棍子
- Sticks POJ1011 hdu1455 (深度优先搜索DFS)
- DFS深度优先搜索(6)--hdu1455(经典深搜+剪枝)
- 09-06 HDU_Steps4.3 DFS剪枝 HDU1010 HDU1016 HDU1426 HDU1455 HDU2510 HDU2553 HDU3290 HDU2616
- hdu1455 && poj1011 Sticks(深度优先搜索 DFS 经典剪枝 详解)
- Hdu1455 Sticks
- 关于jdk与jre的区别
- 使用svn diff的-r参数的来比较任意两个版本的差异
- thinkphp文件上传所需参数
- 写给自己
- SGU131 状态压缩~覆盖方案数
- HDU1455(DFS)
- Ajax版省市三联动(源码)
- Spring使用注解的方式实现AOP
- widger处理点击事件
- Linux 查看进程和删除进程
- JSP中防止重复提交(Javascript)
- 什么是阅读?
- Extjs4 重写Ext.data.TreeStore的load方法,不使多次加载
- VC API常用函数简单例子大全