题意:

给n个木棍,这些木棍是由m个长度均为L的木棍切割而来,求L的最小值。

思路:

DFS+剪枝。

剪枝:

1:L的取值范围在n(max)和n(sum)之间,逐个枚举。sum%L!=0则L不能用。
2:sort对n个木棍长度进行由小到大排序,有以下好处:
a:从大的开始搜索。eg:如果L8;5+3>(优势)5+2+1;把更灵活地木棍留下来。
b:查找更有序。
3:如果有一个木棍无法和其他木棍组合成L,则此种方案不行。<有两种情况>。
4:如果len[i]无法和当前的木棍组成,那么如果len[i+1]=len[i],len[i+1]也不行。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int len[5005],vis[5005];
int n,sum,num,L;
int cmp(int a,int b)
{
return a>b;
}
int DFS(int m,int S,int N)//m代表当前已经组成的木棍数,S代表当前木棍的长度,N等会
{
if(m==num-1)//b:已经有num-1根木棍组合好了,那么剩下的木棍一定也可以组成一根;
return 1;
for(int i=N;i<n;i++){
if(!vis[i]&&(S+len[i])==L)
{
vis[i]=1;
if(DFS(m+1,0,0))
return 1;
vis[i]=0;//虽然上步已经说明这条路行不通,还是要回溯回来原因是下一个else
return 0;//C;
}
if(!vis[i]&&(S+len[i])<L)
{
vis[i]=1;
if(DFS(m,S+len[i],i+1))
return 1;
vis[i]=0;
if(S==0)//说明剩下的木棍无法组成;
return 0;//D;
while(len[i]==len[i+1])//E:此路不通,len[i]不行,如果len[i+1]=len[i],len[i+1]也不行。
++i;
}
}
return 0;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
sum=0;
for(int i=0;i<n;i++){
scanf("%d",&len[i]);
sum+=len[i];
}
sort(len,len+n,cmp);
for(L=len[0];L<=sum/2;L++)//a:减少L的枚举数量,如果L>sum/2,那么只能只有一根木棍
{
if(sum%L==0)//满足L的条件再递归
{
memset(vis,0,sizeof(vis));
num=sum/L;
if(DFS(0,0,0)){
printf("%d\n",L);
break;
}
}
}
if(L>sum/2)
printf("%d\n",sum);
}
return 0;
}

Sticks<DFS>的更多相关文章

  1. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

  2. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  3. hdu 1145(Sticks) DFS剪枝

    Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...

  4. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  5. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...

  6. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  7. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  8. DFS(剪枝) POJ 1011 Sticks

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

  9. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

随机推荐

  1. LeetCode OJ 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  2. Gentoo解决Udev升级的网卡重命名问题

    问题描述: 配置网络时,很多新手运行ifconfig命令查看网卡时,会发现我们熟悉的eth0网卡没有了,或是发现一些不规则命名的东东,不错,那就是你的网卡. 因为内核升级(忘记具体哪个版本了)从ude ...

  3. 使用WTL的消息反射封装CEdit实现监听控件文本改变事件

    消息反射机制可以使对消息的处理都集中在控件类中,以CEdit的EN_CHANGE消息为例: /*MyEdit.h*/ class CMyEdit:public CWindowImpl<CMyEd ...

  4. 使用纯css3实现图片轮播

    <!DOCTYPE html> <html> <head> <title> 飛天网事--纯CSS代码实现图片轮播 </title> < ...

  5. 配置elasticsearch 以及ik分词

    https://github.com/medcl/elasticsearch-analysis-ikhttps://www.elastic.co/downloads/past-releases/ela ...

  6. 二、oracle sql*plus常用命令

    一.sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1).sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限,该用户 ...

  7. C++ 使用string一行一行读取文件

    c++ 读取文件中的一行一行数据 通用模板: std::ifstream in(dictpath); if(!in) { std::cout << __DATE__ << &q ...

  8. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  9. 股票K线图-JfreeChart版

    http://blog.csdn.net/ami121/article/details/3953272 股票K线图-JfreeChart版 标签: jfreechartpropertiesapplet ...

  10. 小Q系列故事——电梯里的爱情

    小Q系列故事——电梯里的爱情 Time Limit : 300/100ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total ...