hdu 1455 Sticks(dfs+可行性剪枝)
来源:互联网 发布:java加泛型的方法 编辑:程序博客网 时间:2024/05/29 08:33
Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6830 Accepted Submission(s): 1995
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
Source
ACM暑期集训队练习赛(七)
题目分析:枚举原始木棒的数量,然后利用深搜判断当前解时候合法,按照从大到小的顺序,如果合法,直接输出
在进行深搜时,当某一个木棒作为一根原始木棒的第一节或者最后一节时都不能成功得到木棒的时候,在当前情况下,就没有继续向下搜索的必要了
因为之后这根木棒一定会落单,不能够组成原始木棒
在进行深搜时,当某一个木棒作为一根原始木棒的第一节或者最后一节时都不能成功得到木棒的时候,在当前情况下,就没有继续向下搜索的必要了
因为之后这根木棒一定会落单,不能够组成原始木棒
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 107using namespace std;int n;int a[MAX];bool used[MAX];int sum;bool flag;void dfs ( int num , int m , int cnt , int total ){ if ( num > sum/total ) return; if ( m == n && cnt == total ) { flag = true; return; } for ( int i = n ; i > 0 ; i-- ) { if ( used[i] ) continue; used[i] = true; if ( num+a[i] == sum/total ) dfs ( 0 , m+1 , cnt+1 , total ); else dfs ( num+a[i] , m+1 , cnt , total ); used[i] = false; if ( flag ) return; if ( num == 0 || num+a[i] == sum/total ) break; }}int main ( ){ while ( ~scanf ( "%d" , &n ),n ) { sum = 0; for ( int i = 1 ; i <= n ; i++ ) { scanf ( "%d" , &a[i] ); sum += a[i]; } sort ( a+1 , a+n+1 ); flag = false; for ( int i = n ; i > 0 ; i-- ) { memset ( used , 0 , sizeof ( used ) ); if ( sum%i == 0 ) { flag = false; dfs ( 0 , 0 , 0 , i ); if ( flag ) { printf ( "%d\n" , sum/i ); break; } } } }}
0 0
- hdu 1455 Sticks(dfs+可行性剪枝)
- HDU 1455 Sticks(DFS+剪枝)
- hdu 1455 sticks dfs+剪枝
- hdu 1455 Sticks 【dfs剪枝】
- hdu 1455 Sticks (dfs 经典剪枝)
- HDU--1455 -- Sticks [DFS] [剪枝优化]
- HDU-1455 Sticks 经典dfs剪枝
- hdu 1455 Sticks(DFS+剪枝)
- hdu 1455 Sticks(DFS+剪枝)
- HDU 1455 Sticks(dfs+强剪枝)
- hdu 2610 Sequence one ( dfs+可行性剪枝 )
- hdu 1455/poj 1011 Sticks(DFS剪枝神题)
- 文章标题 HDU 1455 : Sticks(dfs+剪枝)
- hdu 1445 sticks (经典dfs+剪枝)
- HDU 1455 Sticks 深搜+剪枝
- hdu-1455-Sticks-深搜+剪枝
- 【DFS】hdu 1455 Sticks
- POJ1011-Sticks DFS+剪枝
- Ubuntu开启SSH服务
- 面试题 -- 树
- 给新入职程序员的建议
- Linear-quadratic-Gaussian control
- PHP 分页实现函数
- hdu 1455 Sticks(dfs+可行性剪枝)
- 在centos6.6上安装hadoop完全分布式集群并运行第一个程序
- ActivityManagerService分析一:AMS的启动
- 栈的应用之括号匹配检验
- iOS 动画特效
- 常见算法-将二叉搜索树转化为排序的双向链表(java版)
- Hadoop学习全程记录——eclipse hadoop开发环境配置(2)(修改)
- PHP根据大分类查询所有子类文章包括自身
- 代码狂人——宋某人小传