POJ 1011 sticks 搜索
Sticks
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 125918 Accepted: 29372 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
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0Sample Output
6
5Source
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
int n, arr[maxn], Sum;
bool vis[maxn];
/*cur表示当前选择棍子的序号,diff_len为这根棍子还差多长,
sum_len初始值为总长Sum, cur_len表示搜索当前棍子长度为cur_len的棍子 */
bool dfs(int cur, int diff_len, int sum_len, int cur_len)
{
if (diff_len == )
{
sum_len -= cur_len;
if (sum_len == )//判断是否所有的棍子都取完了
{
return true;
}
for (cur = ; vis[cur]; cur++);//剪枝1,找到剩下的最长的一根,然后接着这个往下找比他小的
vis[cur] = true;
if (dfs(cur + , cur_len - arr[cur], sum_len, cur_len))//从最长的开始继续往下找 这个剪枝的威力很大,从不能TLE直接到A的差距
return true;
vis[cur] = false;
sum_len += cur_len;
}
else
{
for (int i = cur; i < n; i++)
{
/*剪枝2,如果这个棍子的长度等于上一个棍子的长度,并且上一个还不符合要求,
没有取,所以这个肯定也不能取,直接跳过 */
if (i > && arr[i] == arr[i - ] && !vis[i - ])
continue;
if (!vis[i] && diff_len >= arr[i])//如果没有取过,并且需要取的长度大于待取的才行,小小剪枝3,最普通的dfs也该有的
{
vis[i] = true;
diff_len -= arr[i];
if (dfs(i + , diff_len, sum_len, cur_len))//找下一个,普通的dfs, 剪枝4
return true;
diff_len += arr[i];
vis[i] = false;
if (arr[i] == diff_len)//剪枝5,如果走到这里,本次刚好凑成一根棍子,但是既然能走到这,肯定下面的失败了,所以返回上层dfs
break;
}
}
}
return false;
}
bool cmp(const int a, const int b)
{
return a > b;
}
int main()
{
while (cin >> n && n)
{
Sum = ;
for (int i = ; i < n; i++)
{
cin >> arr[i];
Sum += arr[i];
}
sort(arr, arr + n, cmp);//从大到小排序
memset(vis, false, sizeof(vis));
int flag = ;
for (int i = arr[]; i <= Sum / ; i++) //剪枝6,这个为什么是对的,可以举反例,假设要求的i大于Sum/2的话,那么剩下的长度和为Sum-i<i,得出来i+i>Sum,与已知矛盾,所以假设不对。
{
if (Sum % i == )
{
if (dfs(, i, Sum, i))
{
cout << i << endl;
flag = ;
break;
}
}
}
if (!flag)
cout << Sum << endl;
} return ;
}
POJ 1011 sticks 搜索的更多相关文章
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- POJ 1011 Sticks 【DFS 剪枝】
题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- POJ 1011 Sticks(搜索 && 剪枝 && 经典)
题意 : 有n根木棍(n<=64),它们由一些相同长度的木棍切割而来,给定这n根木棍的长度,求使得原来长度可能的最小值. 分析 : 很经典的深搜题目,我们发现答案只可能是所有木棍长度总和的因数, ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- POJ 1011 Sticks(dfs+剪枝)
http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...
随机推荐
- 在Ubuntu12.0.4下搭建TFTP服务器
一.安装相关安装包 tftpd(服务端),tftp(客户端) sudo apt-get install tftp-hpa tftpd-hpa 安装xinetd sudo apt-get install ...
- .Echo 命令中经常提到回显,是什么意思?
C:\>echo on C:\>date /t2006-08-06 星期日 C:\>以上内容是在打开回显的情况下执行的,其实我们想看到的只有2006-08-06 星期日这一行内容,但 ...
- 转 一些shell经验
http://www.cnblogs.com/xublogs/archive/2010/03/16/2292254.html http://www.cnblogs.com/stephen-liu74/ ...
- java Active Object模式(下)
Active Object模式的评价与实现考量 Active Object模式通过将方法的调用与执行分离,实现了异步编程.有利于提高并发性,从而提高系统的吞吐率. Active Object模式还有个 ...
- 使用 HT 单片机芯片做触摸按键的试验:触摸按键实践一
使用 HT 芯片做触摸按键,可供使用的专门用途芯片主要有:HT45R35,HT45R36,HT45R38,原来还有一个 45R34 ,不知道为何停止生产了.如果仅仅是为了按键功能,选择 45R35 觉 ...
- html 商品展示框
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Unity 用C#脚本读取JSON文件数据
读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...
- Gvim自动编译运行c++11的程序
gcc中对c++11的支持是默认不开启的,要想在实现对其的成功编译,需要添加参数-std=c++11: g++ -o test.exe test.cpp g++ -o test.exe test.cp ...
- Light OJ 1017 - Brush (III)
题目大意: 在一个二维平面上有N个点,散落在这个平面上.现在要清理这些点.有一个刷子刷子的宽度是w. 刷子上连着一根绳子,刷子可以水平的移动(在X轴方向上).他可以把刷子放在任何一个地方然后开 ...
- 【转】Java运算符优先级
原文网址:http://www.cnblogs.com/gw811/archive/2012/10/13/2722752.html Java运算符优先级 序列号 符号 名称 结合性(与操作数) 目数 ...