Sticks(Central Europe 1995)

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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

  1. 9
  2. 5 2 1 5 2 1 5 2 1
  3. 4
  4. 1 2 3 4
  5. 0

Sample Output

  1. 6
  2. 5
  1. 题目简单翻译:
  1. 给你一个数n
  1. 然后给你n个小木棍,把他们分成若干组,每组的总长度相等,求最短的总长度,使每组的总长度相等。
  1.  
  1. 思路:
  1. dfs,剪枝
  1. 把它分成若干组,从大到小搜索,使它们成为一组,直到搜到结果。
  1.  
  1. 代码:
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. int n,t[],vis[];
  7. bool Get_Ans;
  8. int Sum;
  9. void dfs(int Length,int Now_Sum,int Now_Groups,int Aim_Groups,int Now_Position)
  10. {
  11. if(Now_Groups==Aim_Groups)//所有的组都收集齐了
  12. {
  13. Get_Ans=true;
  14. return;
  15. }
  16. if(Now_Sum==Length) //收集齐了一组
  17. {
  18. dfs(Length,,Now_Groups+,Aim_Groups,);
  19. }
  20. for(int i=Now_Position;i<n&&!Get_Ans;i++)
  21. {
  22. if(!vis[i]&&t[i]+Now_Sum<=Length)
  23. {
  24. vis[i]=;
  25. dfs(Length,Now_Sum+t[i],Now_Groups,Aim_Groups,i);
  26. if(Get_Ans) return;
  27. vis[i]=;
  28. if(Now_Sum==||Now_Sum+t[i]==Length) return ;//剪枝
  29. }
  30. }
  31. }
  32. bool judge(int Length,int Groups)
  33. {
  34. memset(vis,,sizeof vis);
  35. Get_Ans=false;
  36. dfs(Length,,,Groups,);
  37. return Get_Ans;
  38. }
  39. bool cmp(int a,int b)
  40. {
  41. return a>b;
  42. }
  43. int main()
  44. {
  45. while(scanf("%d",&n)!=EOF&&n)
  46. {
  47. Sum=;
  48. for(int i=;i<n;i++) scanf("%d",&t[i]),Sum+=t[i];
  49. sort(t,t+n,cmp);
  50. int Ans=Sum;
  51. for(int i=n;i>;i--)
  52. if(Sum%i==&&t[n-]<=Sum/i&&judge(Sum/i,i))
  53. {
  54. Ans=Sum/i;
  55. break;
  56. }
  57. printf("%d\n",Ans);
  58. }
  59. return ;
  60. }

Sticks(Central Europe 1995) (DFS)的更多相关文章

  1. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  2. Sticks(UVA - 307)【DFS+剪枝】

    Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...

  3. 2017-2018 ACM-ICPC, Central Europe Regional Contest (CERC 17)

    A. Assignment Algorithm 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string ...

  4. XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--J.Cave

    给你一棵树,现在有m个专家,每个专家计划从$a_i$走到$b_i$, 经过的距离不超过$d_i$,现在让你找一个点,使得所有专家的路途都能经过这个点 令$S_i$表示满足第i个专家的所有点,先检查1可 ...

  5. Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)

    题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...

  6. Gym 101142G : Gangsters in Central City(DFS序+LCA+set)

    题意:现在有一棵树,1号节点是水源,叶子节点是村庄,现在有些怪兽会占领一些村庄(即只占领叶子节点),现在要割去一些边,使得怪兽到不了水源.给出怪兽占领和离开的情况,现在要割每次回答最小的割,使得怪兽不 ...

  7. poj(1011)——Sticks(经典的dfs+剪枝)

    题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. ...

  8. Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights

    一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...

  9. Central Europe Regional Contest 2012 Problem c: Chemist’s vows

    字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: #include<iostream> #include<string> ...

随机推荐

  1. jquery中checkbox全选失效的解决方法

    这篇文章主要介绍了jquery中checkbox全选失效的解决方法,需要的朋友可以参考下     如果你使用jQuery 1.6 ,代码if ( $(elem).attr(“checked”) ),将 ...

  2. Linux_Shell type

    Recommendation is to use the bash shell, because he is strong enough, and absorbed the useful proper ...

  3. TOMCAT之性能跟踪入门

    先扫清前面的障碍,再慢慢进入核心 转一下网上的我关心的话题,实施起来 ~~~ 使用Nginx作为反向代理时,Tomcat的日志记录的客户端IP就不在是真实的客户端IP,而是Nginx代理的IP.要解决 ...

  4. javascript delete机制学习

    想了解delete的机制缘起一个现象,我无法解释,也无法理解. 首先看一下下面这个例子: var x = 1; delete x; //false 然后我又执行了一次: y = 2; delete y ...

  5. paip.输入法编程---智能动态上屏码儿长调整--.txt

    paip.输入法编程---智能动态上屏码儿长调整--.txt 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...

  6. Visual Studio 2015 使用ODP.net进行EF开发

    刚转了新公司,以前公司都是用VS+MSSQL作为开发工具的 现在新公司由于数据库是Oracle,而且新公司比较小规模,开发团队也没有什么规范 访问数据库的方式一直使用ADO.net的DataTable ...

  7. 【桌面虚拟化】之三 Persistent vs NonP

    作者:范军 (Frank Fan) 新浪微博:@frankfan7 在[桌面虚拟化]之二类型及案例中我们探讨了桌面虚拟化的两种架构,HostedVirtual Desktop (VDI) 和 Publ ...

  8. 用户向导页面实现左右滑动的ImageSwitcher

    当你第一次打开app时刻,通常有使用向导现在演示APK基本功能和用法,该向导是非常重要的,用户可以知道并调整到速度app如何. 实现此使用向导有非常多种方法,比方用ImageSwitcher.View ...

  9. Android应用程序启动过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程 ...

  10. Oracle:grouping和rollup

    Oracle grouping和rollup简单测试 SQL,,,) group by department_id order by department_id; DEPARTMENT_ID SUM( ...