题目链接

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

9

5 2 1 5 2 1 5 2 1

4

1 2 3 4

0

Sample Output

6

5

AC

  • 剪枝的运用
  • 首先从最长的木棒枚举,满足木棒总和是枚举木棒的倍数
  • 然后dfs看能否把所有木棒用完,如果用完就找到了一个最小的合适值
  • 开始剪枝
  • 如果进行一系列的搜索之后,发现当前这个木棒未完成匹配,而且当前所要匹配的是整个目标,这就不用接着往下dfs
    举个例子:木棒分别是 6 5 5 3 2 2, 你要找6, 第一个可以,接着匹配下一个6,找到5时发现没有匹配的这时候直接退出就行,说明6不能构成完整的
#include <bits/stdc++.h>
using namespace std;int a[65];
bool vis[65];
bool cmp(int a, int b) {return a > b;
}
bool dfs(int n, int tar, int temp, int len, int now) {if (len == n)   return true;if (temp == 0)  temp = tar, now = 0;for (int i = now; i < n; ++i) {if (vis[i]) continue;if (a[i] <= temp) {vis[i] = true;if (dfs(n, tar, temp - a[i], len + 1, i + 1))return true;else{vis[i] = false;if (temp == tar)return false;}}} return false;
}
int main() {int n;while(scanf("%d", &n), n) {int sum = 0;for (int i = 0; i < n; ++i) {scanf("%d", &a[i]);sum += a[i];}sort(a, a + n, cmp);int tar = a[0];while (1) {memset(vis, false, sizeof(vis));while (sum % tar)   tar++;if(dfs(n, tar, tar, 0, 0))  break;tar++;}printf("%d\n", tar);}return 0;
} 

HDU Problem - 1455 Sticks相关推荐

  1. HDU 1455 Sticks

    DFS+剪枝!!!! ! 这是继上一个,做的加难的一个.难了一点点! ! 非常幸运0MS过!! AC代码例如以下: ///dfs+剪枝 0MS 236K#include<iostream> ...

  2. hdu 1455 java_hdu 1455 Sticks

    #include #include #include using namespace std; int cmp(int a,int b) { return a>b; } int s[70],n, ...

  3. HDU Problem 1272 小希的迷宫 【并查集】

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  4. HDU Problem 2062 Bone Collector【01背包】

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. HDU Problem - 1533 Going Home(费用流板子题)

    题目链接 Problem Description On a grid map there are n little men and n houses. In each unit time, every ...

  6. HDU Problem - 5918 Sequence I

    题目链接 Problem Description Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,ana_1,a_2,\cdots ,a_n and b1,b ...

  7. HDU Problem - 5976 Detachment(逆元,阶乘打表,数学)

    题目链接 Problem Description In a highly developed alien society, the habitats are almost infinite dimen ...

  8. HDU Problem - 5971 Wrestling Match(染色)

    题目链接 Problem Description Nowadays, at least one wrestling match is held every year in our country. T ...

  9. HDU Problem - 5935 Car(模拟)

    题目链接 Problem Description Ruins is driving a car to participating in a programming contest. As on a v ...

最新文章

  1. 随机文件名生成可用于文件上传(图片)
  2. 我就是古董店的那个店小二……专司收藏……
  3. 视力差,不要怕!PNAS:服用超长链多不饱和脂肪酸可显著改善视觉和视网膜功能!...
  4. 【转】Java和C++的区别有哪些?
  5. Mac系统升级node
  6. 42. HTTP Cookie
  7. oracle 删除数据_Oracle海量数据表标准删除方案--分步分阶段大表删除
  8. CLR寄宿(中) 托管exe文件的加载和执行
  9. Packet Tracer 5.0建构CCNA实验攻略2配置vlan
  10. /proc/sys/vm/drop_caches
  11. 为缺少调色板的png图片添加调色板
  12. Codeforces Round #685 (Div. 2) (D(博弈) E1 E2(交互))
  13. 单片机里程计量设计c语言,基于单片机的出租车计价器的里程计算设计
  14. Redis Desktop Manager 黑屏
  15. opencv图像处理学习(六十八)——肤色检测
  16. 不看会后悔的100个超有趣冷知识(一)
  17. 派森python_派森(Python)
  18. 阿里云 blink 相关错误码问题解决
  19. 应用技术大公开系列Q之八:(纳米纤维).石墨烯滤芯的制备工艺 (*9-2)
  20. ABC247ABCD

热门文章

  1. 使用I/O 系统调用--copy.c
  2. 【网络流】网络流小总结
  3. Windows Server 2008 R2 负载平衡入门篇
  4. Object-C 入门
  5. 如何让你的百万级SQL运行得更快 else
  6. [网络安全自学篇] 二十七.Sqlmap基础知识、CTF实战及请求参数设置(一)
  7. 2019年第十届蓝桥杯 - 省赛 - C/C++大学C组 - D. 质数
  8. PYTHON如何在内存中生成ZIP文件
  9. Circular Sequence UVA - 1584
  10. 百练1724:ROADS