CSU-ACM2017暑期训练4-dfs G - Sticks HDU - 1455
2017-07-29 17:30
459 查看
题目:
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 wereoriginally. 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.
InputThe 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.
OutputThe 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
题意:给你一串数字,将他们中的有些数字相加,得到另一串数字他们的值全为k,求这个k的最小值。
思路:显然k的范围是这串数字的最大值到这串数字的总和,然后对这个值进行判断,看这串数字能不能通过变换使其全为k。这里用到搜索。这题搜索的时候需要用剪枝的地方特别多,不然很容易超时,具体见代码。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1000000000; const int maxn = 100; int a[66],vis[66]; int T,n,m; int sum,len,flag,tot,can; bool cmp(int a,int b) { return a>b; } void dfs(int res,int step,int pos)//res:要凑齐tot还需要的数量.setp:搜索过的木棍总数.pos:当前搜索到的位置 { if(flag)//标记成功与否 return; if(step==n) { flag=1; return; } if(res==0&&step<n) dfs(tot,step,0); for(int i=pos;i<n;i++) { if(!vis[i]&&!flag&&a[i]<=res) { vis[i]=1; dfs(res-a[i],step+1,i+1); vis[i]=0; if(res==tot)//第1次剪枝:如果剩下的res还有tot这么长说明最大的木棍也就是当前这根不能用,直接停止搜索 return; while(a[i]==a[i+1])//第2次剪枝:如果下一次搜到的和这一次一样,则直接跳过. i++; } } } int main() { while(scanf("%d",&n)!=EOF&&n) { sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n,cmp); for(int i=a[0];i<=sum;i++) { if(sum%i) continue; memset(vis,0,sizeof(vis)); flag=0; tot=i; dfs(tot,0,0); if(flag) { printf("%d\n",i); break; } } } return 0; }
相关文章推荐
- CSU-ACM2017暑期训练1-Debug与STL A - Surprising Strings
- Function Run Fun--CSU-ACM2017暑期训练3-递推与递归
- CSU-ACM2017暑期训练3 J - Non-boring sequences UVA - 1608
- CSU-ACM2017暑期训练5-三分 E - Line belt HDU - 3400
- CSU-ACM2017暑期训练10-并查集&&HASH F - Flying to the Mars HDU - 1800(字符串hash)
- CSU-ACM2017暑期训练16-树状数组 A - Ultra-QuickSort POJ - 2299
- CSU-ACM2017暑期训练1-Debug与STL C - Languages
- CSU-ACM2017暑期训练3-递推与递归I - Erratic Expansion UVA - 12627
- CSU-ACM2017暑期训练4-dfs D - 单源最大权路径 CSU - 1960
- CSU-ACM2017暑期训练6-bfsH - ACM小组的古怪象棋 CSU - 1224
- CSU-ACM2017暑期训练1-Debug与STL E - Train Problem I
- CSU-ACM2017暑期训练3-递推与递归 J - Non-boring sequences
- CSU-ACM2017暑期训练4-dfs E - The Settlers of Catan POJ - 2258
- CSU-ACM2017暑期训练14-最短路 D - 输入量很大的模板题
- CSU-ACM2017暑期训练16-树状数组 C - 按钮控制彩灯实验 CSU - 1770
- CSU-ACM2017暑期训练3-递推与递归 D-Function Run Fun POJ-1579
- CSU-ACM2017暑期训练4-dfs F - Twenty-four point CSU - 1600
- CSU-ACM2017暑期训练7-模拟&&贪心 A - Radar Installation POJ - 1328
- CSU-ACM2017暑期训练1-Debug与STL hdu2736 - Surprising Strings
- CSU-ACM2017暑期训练3-递推与递归 H - Race UVA - 12034