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 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...
随机推荐
- php获取https下的内容
直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...
- tableview 编辑状态设置
#pragma mark - tableview 编辑状态设置 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSI ...
- vs2013update4 vs-mda-remote cordova真机测试ios 解决里面一个坑
sudo npm install -g vs-mda-remote --user=你的用户名 此步骤为安装vs-mda-remote,如果安装成功 执行vs-mda-remote –secure fa ...
- Linux 查看 80 端口的占用情况
lsof -i:端口号 eg: lsof -i:80 lsof -i:21 [root@localhost ~]# lsof -i: COMMAND PID USER FD TYPE DEVICE S ...
- j
在Java程序或JSP程序中,其实有很多的代码段是可以重复使用的,比如对数据库的操作.用户的有效性检查及某些项特定功能的实现等.为了很好的解决这个问题,提高开发效率,Sun公司推出了JavaBean, ...
- operation 多线程
2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情.Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOperati ...
- hadoop各版本下载
http://hadoop.apache.org/ Download Hadoop from the release page. http://hadoop.apache.org/releases.h ...
- MySQL表损坏预防与修复
1. 表损坏的原因分析 以下原因是导致mysql 表毁坏的常见原因: 1. 服务器突然断电导致数据文件损坏. 2. 强制关机,没有先关闭mysql 服务. 3. mysqld 进程在写表时 ...
- linux 里 /etc/passwd 、/etc/shadow和/etc/group 文件内容解释
•/etc/passwd文件用于存放用户账户信息,每行代表一个账户,每个账户的各项信息用冒号分割,例如: root:x:::root:/root:/bin/bash username:password ...
- dll文件已经引用,但using找不到命名空间
一:问题截图 二:解决办法 1.没看到lz的代码中有Vancl.Server的dll. 2.确实有编译不过的问题,是Vancl.WindowsServices这个工程的target framework ...