Sticks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 113547   Accepted: 26078
问题描述
  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.
输入格式
  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.
输出格式
  The output should contains the smallest possible length of original sticks, one per line.
样例输入
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
样例输出
6
5
  1. #include<iostream>
  2. #include<cstring>
  3. #include<iomanip>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. int vis[]={};
  8. int is_end=;//代表结束
  9. int sum;//总长度
  10. int n; //总数量
  11. int len;
  12. int a[];
  13. bool com(int a,int b)
  14. {
  15. return a>b;
  16. }
  17.  
  18. void dfs(int used_num,int now_len,int now_index)//已使用的木棒总数,目前的长度,目前木棒的编号
  19. {
  20. if(is_end==)
  21. return;
  22. if(now_len==len)
  23. {
  24. if(used_num==n)
  25. is_end=;
  26. else
  27. dfs(used_num,,);//代表拼成一个原始长度的了,继续拼下一个
  28. return;
  29. }
  30. else if(now_len==)
  31. {
  32. while(vis[now_index]==)
  33. now_index++;
  34. vis[now_index]=;
  35. dfs(used_num+,a[now_index],now_index+);
  36. vis[now_index]=;
  37. }
  38. else
  39. {
  40. for(int i=now_index;i<n;i++)
  41. {
  42. if(vis[i]==&&now_len+a[i]<=len)
  43. {
  44. if(vis[i-]==&&a[i-]==a[i])//前一个和这个大小一样,而且不是同一次的dfs,就跳过 :剪枝
  45. continue;
  46. vis[i]=;
  47. dfs(used_num+,now_len+a[i],i+);
  48. vis[i]=;// 一定要有,在上面dfs没有找到以后,重新设置为没有访问过。
  49. }
  50. }
  51. }
  52. return;
  53. }
  54. int main()
  55. {
  56. while(cin>>n&&n!=)
  57. {
  58. sum=;
  59. for(int i=;i<n;i++)
  60. {
  61. cin>>a[i];
  62. sum+=a[i];
  63. }
  64. is_end=;
  65. sort(a,a+n,com);//从大到小排列
  66. for(len=a[];len<=sum;len++)//从最大的棒到总长度枚举
  67. {
  68. if(sum%len!=)
  69. continue;//总长度一定是原始长度的整数倍
  70. else
  71. {
  72. memset(vis,,sizeof(vis));
  73. dfs(,,);
  74. if(is_end==)
  75. break;
  76. }
  77. }
  78. cout<<len<<endl;
  79. }
  80. return ;
  81. }

dfs+剪枝 poj1011的更多相关文章

  1. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. HZNU-ACM寒假集训Day11小结 贪心

    1.刘汝佳紫书区间问题三大情况 1.选择不相交区间 贪心策略:一定要选择第一个区间 2.区间选点问题 贪心策略:取最后一个点 3.区间覆盖问题: n个闭区间,选择尽量少的区间覆盖一条指定线段[s,t] ...

  2. ACM-Divide Tree

    题目描述:Divide Tree   As we all know that we can consider a tree as a graph. Now give you a tree with n ...

  3. DW1000芯片定位技术解析

    近些年来随着物联网和机器人技术的大发展,精确定位技术的热度也随之攀升.目前精确定位的技术有很多,如基于wifi.RFID.zigbee.超声波.UWB等技术都可以实现精准定位.由于技术的不同,精度也不 ...

  4. Python模块进阶、标准库、扩展库

    模块进阶 Python有一套很有用的标准库(standard library).标准库会随着Python解释器,一起安装在你的电脑中的. 它是Python的一个组成部分.这些标准库是Python为你准 ...

  5. Java IO 乱码

    InputStreamReader isr = new InputStreamReader(new FileInputStream("./test/垃圾短信训练集80W条.txt" ...

  6. 利用libpcap抓取数据包

    转载自:http://blog.csdn.net/tennysonsky/article/details/44811899 概述 libpcap是一个网络数据包捕获函数库,tcpdump就是以libp ...

  7. LVS三种模式区别

    参考文档 http://www.magedu.com/65436.html 名词:CIP 客户端IP地址   VIP:即DS服务器上的代理IP地址,也是客户端访问的执行IP地址 1.NAT模式 ①.客 ...

  8. <强化学习>马尔可夫决策过程MDP

    一.MDP  / NFA    :马尔可夫模型和不确定型有限状态机的不同 状态自动机:https://www.cnblogs.com/AndyEvans/p/10240790.html MDP和NFA ...

  9. 吴裕雄--天生自然MySQL学习笔记:MySQL 处理重复数据

    有些 MySQL 数据表中可能存在重复的记录,有些情况允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 防止表中出现重复数据 可以在 MySQL 数据表中设置指定的字段为 PRIMARY K ...

  10. sersync配置

    只记录了自己的操作步骤,详细信息也可以参考https://www.cnblogs.com/sellsa/p/5345092.html 安装sersync 将sersync安装包解压后会有两个配置文件 ...