Sticks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 113547   Accepted: 26078
问题描述
  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.
输入格式
  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.
输出格式
  The output should contains the smallest possible length of original sticks, one per line.
样例输入
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
样例输出
6
5
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
using namespace std;
int vis[]={};
int is_end=;//代表结束
int sum;//总长度
int n; //总数量
int len;
int a[];
bool com(int a,int b)
{
return a>b;
} void dfs(int used_num,int now_len,int now_index)//已使用的木棒总数,目前的长度,目前木棒的编号
{
if(is_end==)
return;
if(now_len==len)
{
if(used_num==n)
is_end=;
else
dfs(used_num,,);//代表拼成一个原始长度的了,继续拼下一个
return;
}
else if(now_len==)
{
while(vis[now_index]==)
now_index++;
vis[now_index]=;
dfs(used_num+,a[now_index],now_index+);
vis[now_index]=;
}
else
{
for(int i=now_index;i<n;i++)
{
if(vis[i]==&&now_len+a[i]<=len)
{
if(vis[i-]==&&a[i-]==a[i])//前一个和这个大小一样,而且不是同一次的dfs,就跳过 :剪枝
continue;
vis[i]=;
dfs(used_num+,now_len+a[i],i+);
vis[i]=;// 一定要有,在上面dfs没有找到以后,重新设置为没有访问过。
}
}
}
return;
}
int main()
{
while(cin>>n&&n!=)
{
sum=;
for(int i=;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
is_end=;
sort(a,a+n,com);//从大到小排列
for(len=a[];len<=sum;len++)//从最大的棒到总长度枚举
{
if(sum%len!=)
continue;//总长度一定是原始长度的整数倍
else
{
memset(vis,,sizeof(vis));
dfs(,,);
if(is_end==)
break;
}
}
cout<<len<<endl;
}
return ;
}

dfs+剪枝 poj1011的更多相关文章

  1. poj1011(DFS+剪枝)

    题目链接:https://vjudge.net/problem/POJ-1011 题意:给定n(<=64)条木棍的长度(<=50),将这些木棍刚好拼成长度一样的若干条木棍,求拼出的可能的最 ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

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

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  2. 十三、react-router 4.x的基本配置

    路由的定义及作用 根组件根据客户端不同的请求网址显示时,要卸载上一个组件,再挂载下一个组件,如果手动操作话将是一个巨大麻烦.具体过程如下图: [根组件] ↑ [首页组件] [新闻组件] [商品组件] ...

  3. C# Stream篇(六) -- BufferedStream

    BufferedStream 目录: 简单介绍一下BufferedStream 如何理解缓冲区? BufferedStream的优势 从BufferedStream 中学习装饰模式 如何理解装饰模式 ...

  4. UVA - 818 Cutting Chains(切断圆环链)(dfs + 二进制法枚举子集)

    题意:有n个圆环(n<=15),已知已经扣在一起的圆环,现在需要打开尽量少的圆环,使所有圆环可以组成一条链. 分析:因为不知道要打开哪个环,如果列举所有的可能性,即枚举打开环的所有子集,最多才2 ...

  5. 关于数据库text字段

    问题描述: maven项目中,使用MBG代码生成器自动生成的实体类对象时,当数据库中表的字段有 text 类型时,对应到java类中是String类型的,在前端页面通过ajax获取到 json 格式的 ...

  6. MFC 屏蔽esc跟enter键

    BOOL CMenuOperate::PreTranslateMessage(MSG* pMsg) { if(pMsg->message == WM_KEYDOWN && pMs ...

  7. 20180122 PyTorch学习资料汇总

    PyTorch发布一年团队总结:https://zhuanlan.zhihu.com/p/33131356?gw=1&utm_source=qq&utm_medium=social 官 ...

  8. 很多内容是转载或copy各个大佬的

    很多内容是转载或copy各个大佬的

  9. 面试题(6)之 leetcode-001

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  10. 第42章 AWR报表的使用

    第42章 AWR报表的使用exec dbms_gather.gather_table_stats('scott','emp');exec dbms_gather_gather_index_stats( ...