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. PyCharm下创建并运行我们的第一个Django项目

    PyCharm下创建并运行我们的第一个Django项目 准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm 1. 创建一个新的工程 第一次运行Pycha ...

  2. css 径向渐变

    .example { width: 150px; height: 80px; background: -webkit-radial-gradient(red, green, blue); /* Saf ...

  3. java初学小项目-酒店客房管理系统

    最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统 /* 酒店客房管理系统 */ import java.util.Scanner;//通过键盘来输入命令需要的引 ...

  4. 大数据高可用集群环境安装与配置(06)——安装Hadoop高可用集群

    下载Hadoop安装包 登录 https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 镜像站,找到我们要安装的版本,点击进去复制下载链接 ...

  5. filter滤镜兼容ie的rgba属性

    要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,0.1); 但是要兼容到ie8.这个就有点 ...

  6. 三、CI框架之一个最简单的页面

    一.在CI框架里面的controllers <?php defined('BASEPATH') OR exit('No direct script access allowed'); class ...

  7. IIS7实现访问HTTP定向至HTTPS访问

    工具及软件: 系统:windows2008R2 软件:IIS7.0,IIS的Microsoft URL重写模块2.0 下载 操作步骤: 1.下载并在IIS中安装Microsoft URL重写模块2.0 ...

  8. Apache的网站,使用Nginx进行反向代理(1个IP绑定多个域名,对应多个网站)解决方案

    同一个端口是不能同时有两个程序监听的.所以换个思路解决同一台服务器下某些网站运行在nginx下,某些网站运行在Apache下共存. 解决思路: 将nginx作为代理服务器和web服务器使用,nginx ...

  9. 使用docker-sync解决docker for mac 启动的虚拟容器程序运行缓慢的问题

    背景: 新入职的公司有个非常OG的大项目,为了避免新同学重复造轮子,有哥们已经把项目需要的所有打好了一个镜像供我们启动docker. 初次启动docker 使用的命令如下: docker run -i ...

  10. SAP_BASIS常用事务代码

    1.SM66:监控当前系统的所有进程: 2.SM50/SM51:监视当前客户端的所有进程: 3.AL08:查看系统当前用户登录情况: 4.ST22:查看系统发生的DUMP: 5.ST03:查看系统当前 ...