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. junit基础学习之-断言注解(3)

    断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过. 断言核心方法 assertArrayEquals(expecteds, actuals) 查看两个数组是否 ...

  2. 【剑指Offer】面试题07. 重建二叉树

    题目 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 ...

  3. Linux每日一练20200221

  4. windows Driver 查询指定键值

    NTSTATUS status; HANDLE hKey = NULL; OBJECT_ATTRIBUTES oa; UNICODE_STRING strPath = RTL_CONSTANT_STR ...

  5. 洛谷 P5146 最大差值

    题目传送门 好水的题... AC代码: #include<iostream> #include<cstdio> using namespace std; ,a,ans = -; ...

  6. 通过SQL语句操作Sqlite数据库

    一.数据库的创建 数据库版本为1 //Ctrl+Shift+U:大写 public static final String DATABASE_NAME ="zzw.db"; pub ...

  7. [Python函数]encode,decode

    前言: 我们知道,计算机是以二进制为单位的,也就是说计算机只识别0和1,也就是我们平时在电脑上看到的文字,只有先变成0和1,计算机才会识别它的意思.这种数据和二进制的转换规则就是编码.计算机的发展中, ...

  8. JAVA中汉字的Compare排序

    当调用String.compare方法的时候,比较的是Unicode码,并不能对汉字进行准确的排序,所以汉字比较时会出现比较混乱的结果. java.text.Collator类中有一个getInsta ...

  9. Tensorflow学习教程------代价函数

    Tensorflow学习教程------代价函数   二次代价函数(quadratic cost): 其中,C表示代价函数,x表示样本,y表示实际值,a表示输出值,n表示样本的总数.为简单起见,使用一 ...

  10. ES6 之 Math对象的扩展

    1.ES5 http://www.w3school.com.cn/jsref/jsref_obj_math.asp 2.ES6 Math.trunc() - 取整,去掉一个数的小数部分 console ...