poj1011 Sticks (dfs剪枝)
【题目描述】
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.
【题目链接】
【算法】
枚举答案对每一种情况构造+判定。剪枝技巧:
1. 优化搜索顺序
2. 排除等效冗余
(1)构造选取的棍子递减的取
(2)初始为0的棍子若无法构造出答案,则该分支减去
(3)构造选取的棍子不重复选择同样长度的已经失败过的棍子。
【代码】
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int n,all,lim;
int a[100],v[100];
bool dfs(int cur,int len,int last) {
if(cur>all) return 1;
if(len==lim) return dfs(cur+1,0,1);
int fail=0;
for(int i=last;i<=n;i++) {
if(!v[i]&&len+a[i]<=lim&&a[i]!=fail) {
v[i]=1;
if(dfs(cur,len+a[i],i+1)) return 1;
fail=a[i];
v[i]=0;
if(len==0) return 0;
}
}
return 0;
}
int main() {
while(~scanf("%d",&n)&&n) {
int sum=0,val=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i],val=max(a[i],val);
sort(a+1,a+n+1);
reverse(a+1,a+n+1);
for(lim=val;lim<=sum;lim++) {
if(sum%lim==0) {
all=sum/lim; memset(v,0,sizeof(v));
if(dfs(1,0,1)) break;
}
}
printf("%d\n",lim);
}
return 0;
}
poj1011 Sticks (dfs剪枝)的更多相关文章
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- poj1011(DFS+剪枝)
题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...
- poj 1011 :Sticks (dfs+剪枝)
题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...
- POJ1011 (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 129606 Accepted: 30388 Descrip ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- poj1011 && uva307 DFS + 剪枝
将木棒从大到小排列,保证每次的选择都是最长可选的木棒. 剪枝: 1 . 如果第 i 根木棒被选择却无法成功拼接,那么后面与其长度相同的也不能选择. 2 . 如果第 cnt + 1 根木棒无法成功拼接, ...
- hdu 1145(Sticks) DFS剪枝
Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
随机推荐
- Ldap 从入门到放弃(二)
OpenLDAP 服务器安装与配置 本文内容是自己通过官网文档.网络和相关书籍学习和理解并整理成文档,其中有错误或者疑问请在文章下方留言. 一.概述 本文以Centos 6.8(64bit)为例介绍 ...
- table表格 td设置固定宽度
table宽度自适应,而且部分TD是固定宽度. 只需要将固定宽设死,留下一列不设置宽度,将table宽度设置为100%. table-layout:fixed 作用不是很清楚 <table wi ...
- 线程协作之threading.Condition
领会下面这个示例吧,其实跟java中wait/nofity是一样一样的道理 import threading # 条件变量,用于复杂的线程间同步锁 """ 需求: 男:小 ...
- android 7.0适配(总结)
file_paths.xml <?xml version="1.0" encoding="utf-8"?><paths xmlns:andro ...
- 跳转控制语句continue
1 continue的使用场景: 1.1 在循环语句中 注意:离开使用场景的存在是没有意义的 2 continue的作用: 2.1 单层循环对比break,然后总结两者的区别 2.1.1 break ...
- 区间查询异或最大值——cf1100F,hdu6579
cf1100F是静态区间查询最大值,有离线的解法,我感觉线段树或者莫队应该都能过 更优秀的解法可以在线并支持修改,可以解决hdu6579,即依次插入每个数,pos[i][j]表示在插第i个数时第j个基 ...
- Kohana Minion cli 学习
1.E:\html\tproject\framebota\platform\bootstrap.php Kohana::modules(array( 'auth' => MODPATH.'aut ...
- XXX is not a function
今天,一个以前的小伙伴跟我说他遇到了一个问题,调试了将近两天(这家伙一开始不打算干程序员,跑去干了两个月销售,现在又想回来写代码了,所以就自己折腾一个demo,免得面试的时候被问住) 我把他的代码从头 ...
- 登陆一个系统时,前端js实现的验证,记住密码等功能
记住密码部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
- 手把手教你搞定个推iOS推送SDK集成
以下是一位开发者在集成个推iOS推送SDK过程中的真实经历. 作者:Ezreallp 一次偶然的机会,公司的项目要用到推送,我自己本来就很懒,不愿意去弄整套APNS的流程,刚好之前跟朋友聊起过他们的产 ...