Sticks
来源:互联网 发布:淘宝怎么改图片尺寸 编辑:程序博客网 时间:2024/06/05 15:31
Sticks
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 20 Accepted Submission(s) : 7
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 should 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
PKU
思路:搜索+剪枝
1.sum是木材的长度和,len是原来木材的长度,n 是给定的木材根数,i 是原来的木材根数,data[n-1]是给定木材的最大长度,那么sum/data[n-1] 大于等于sum/len;
所以原来最短木材为sum/i;
2.如果每次的第一个木材不能找到其它木材组成目标长度,说明无法在当前组合方式下组合。
3.由于所有棒子已降序排序,在DFS时,若某根棒子不合适,则跳过其后面所有与它等长的棒子;
4.如加人一根木棍没有达到len,则下次在i-1个棍子中搜索
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n , sum;int data[70];int vist[70];int len;bool dfs(int temp , int time,int total , int x){ //printf("bfs\n"); if(time == total)return true; int same = -1; for(int i = x; i >= 0; i --) { if(vist[i] || data[i] == same)continue; if(!vist[i]) { vist[i] = 1; if(temp+data[i] == len) { if(dfs(0 , time+1 , total , n-1)) return true; else same = data[i]; } else if(temp+data[i] < len) { if(dfs(temp+data[i] , time ,total,i-1)) return true; else same = data[i]; } vist[i] = 0; } if(temp == 0)break; } return false;}int main(){ while(scanf("%d",&n),n) { sum = 0; for(int i = 0; i < n; i ++) { scanf("%d",&data[i]); sum +=data[i]; } sort(data , data+n); for(int i = sum/data[n-1]; i >= 1; i --) { if(sum % i== 0 && data[n-1]<=sum/i) { memset(vist, 0 , sizeof(vist)); len = sum / i; if(dfs(0,0,i-1, n-1)){ printf("%d\n",len); break; } } } } return 0;}
0 0
- Sticks
- sticks
- sticks
- sticks
- Sticks
- sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- sticks
- Sticks
- 1011 Sticks
- 理解Javascript的闭包
- 详解Android中AsyncTask的使用
- c#:从DataTable中抽取Distinct数据
- 恢复SVN中删除的文件或文件夹
- Asterisk 1.4.0 CLI commands
- Sticks
- 自己动手写代码,整合Spring和Hibernate(三)之DAOSupport和DAOTemplate
- hashmap排序
- 2014-4-14~2014-4-20工作总结
- 一步一步写算法(之hash表)
- 年轻人需谨记的创富理念
- KMPKMP字符串模式匹配详解
- Android FrameWork中的SP、RefBase、weakref_impl,Thread类
- OUC_2014 Spring Training 省赛组队训练赛 #1总结