hdu--1455--DFS(难度一般)
来源:互联网 发布:知乎娱乐圈猛料2017 编辑:程序博客网 时间:2024/05/18 21:08
Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4757 Accepted Submission(s): 1320
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 total,L;int num,sum;typedef struct{ int length; bool mark;}Sticks;Sticks sticks[70];bool cmp(Sticks a,Sticks b){ return a.length>b.length;}//s 已组成的木棒数目,len已经组成的长度,pos搜索的木棒的下标的位置int dfs(int s,int len,int pos) //(1,0,-1){ if(s==total)return 1; for(int i=pos+1;i<num;i++) { if(sticks[i].mark)continue; if(len+sticks[i].length == L) { sticks[i].mark = true; if(dfs(s+1,0,-1)){ return true; } sticks[i].mark = false; return false; }else if(sticks[i].length+len<L){ sticks[i].mark = true; if(dfs(s,len+sticks[i].length,i)){ return true; } sticks[i].mark = false; if(len==0)return false; while(sticks[i].length==sticks[i+1].length)i++; } } return false;}int main(){ while(scanf("%d",&num)&&num!=0) { sum = 0; for(int i = 0; i < num; i++) { scanf("%d",&sticks[i].length); sum += sticks[i].length; sticks[i].mark = false; } sort(sticks,sticks+num,cmp); for(L = sticks[0].length; L <= sum; L++) { if(sum%L!=0)continue; total = sum / L; if(dfs(1,0,-1)) { printf("%d\n",L); break; } } } return 0;}
- hdu--1455--DFS(难度一般)
- hdu 5700 难度:一般
- hdu 2795 难度:一般
- 百度Android笔试题(一般难度)
- hdu 5723 最小生成树+数学期望 难度:一般
- poj 3321 难度:一般
- poj 3928 难度:一般
- Codeforces 263 C. Circle of Numbers D. Cycle in Graph (dfs一般难度)
- (一般)POJ-2718 DFS套DFS
- hdu 1455 (DFS+剪枝)
- hdu KM小集(中低难度)
- HDU 4546 比赛难度(优先队列)
- hdu 4034 floyd变形(入门难度)
- (一般)POJ-1129 染色,DFS技巧
- hdu 1455 Sticks (dfs 经典剪枝)
- hdu 1455 Sticks(经典DFS)
- hdu 1455 Sticks(DFS+剪枝)
- hdu 1455 Sticks(DFS+剪枝)
- 无限的路_hdu_2073(PE).java
- 动态规划之 矩阵链乘法
- TEST BLOG
- Android基于回调的事件处理
- RTSP ,RTCP ,RTP区别和联系
- hdu--1455--DFS(难度一般)
- CodeIgniter 配置使用smtp服务发送html邮件
- Metatable In Lua 浅尝辄止
- 博弈论的一些基础知识(参考网络资料,学习总结,很好,分享并保存)
- hibernate入门示例
- IO调度器
- C基础:数组实现堆栈倒序打印
- exit()函数
- hdu 4655 (组合数)