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. Python 加载mnist、cifar数据

    import tensorflow.examples.tutorials.mnist.input_data mnist = input_data.read_data_sets("MNIST_ ...

  2. hdu 1160 上升序列 dp

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. python---生成式

    1.[(x,y) for x in [1,2,3] for y in [4,2,3] if x == y] (x,y):输出表达式,产生最终列表的元素 for x in [1,2,3] for y i ...

  4. P 1025 链表反转

    转跳点:

  5. 实战 迁移学习 VGG19、ResNet50、InceptionV3 实践 猫狗大战 问题

    实战 迁移学习 VGG19.ResNet50.InceptionV3 实践 猫狗大战 问题   参考博客:::https://blog.csdn.net/pengdali/article/detail ...

  6. Windows桌面图标不见了,可能是结束了explorer.exe进程导致

    Windows桌面图标不见了,怎么办?那么可能是你关掉了explorer.exe的进程. 解决办法: ① Ctrl+shift+delete打开任务管理器,查看进程是否有explorer.exe ② ...

  7. promise核心6 自定义promise

    1.定义整体结构(不写实现) 定义一个自己的promise的库 lib(库的简写) 一个js文件.一个js模块(不能用es6  也不能commjs)(用es5模块语法 ) 匿名函数自调用.IIFE ( ...

  8. Javascript声明和使用变量

    1.1变量的声明 要在程序中使用变量,就必须从声明变量学起,因为Javascript语法与我们基础的其他程序语言声明变量的方法略有不同,但是Javascript语法的变量应用非常强大,使用也非常简单. ...

  9. python3+Opencv 搭建环境和基本操作

    一.必备前提: Python3.5及以上版本.pip.windows环境 二.搭建opencv 该部分可以创建隔绝的Python环境来引入,参照virtualenv的使用 在目标的cmd窗口,依次输入 ...

  10. maven常用配置setting.xml详解

    参考文章: https://www.cnblogs.com/hwaggLee/p/4579418.html 1.<localRepository/> 该值maven本地仓库的路径 < ...