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. Java中的String介绍

    一.概述 String是代表字符串的类,本身是一个最终类,使用final修饰,不能被继承. 二.String字符串的特征 1. 字符串在内存中是以字符数组的形式来存储的. 示例如下,可以从String ...

  2. taro编译的时候报 exports.pRimraf = util_1.promisify(rimraf); 错误

    C:\Users\1\AppData\Roaming\npm\node_modules\@tarojs\cli\dist\h5\helper.js:8 exports.pRimraf = util_1 ...

  3. /etc/apt/sources.list.d

    deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main# deb-src http://ppa.launchpad.net/w ...

  4. 【pwnable.kr】passcode

    pwnable从入门到放弃,第六题. ssh passcode@pwnable.kr -p2222 (pw:guest) 完全是‘&’的锅. #include <stdio.h> ...

  5. BZOJ:2815: [ZJOI2012]灾难

    题解: 构造灭绝树: x指向的点表示x的祖先死亡则x死亡 动态LCA: 可以用LCT维护或直接更新倍增数组 最后统计子树点的个数 坑: 我还不会序列型Toposort #include<iost ...

  6. Hibernate(八)--session的两种获取方式

    openSession getCurrentSession Hibernate有两种方式获得session,分别是: openSession和getCurrentSession他们的区别在于1. 获取 ...

  7. 51Nod 1067 Bash博弈V2

    这道题告诉我,一定要去尝试,去推算,不要动不动就找度娘要答案.(惭愧惭愧) 既然是博弈问题,按理我们应该找出规律,怎么找呢,推,把前几项写出来找规律,动手很重要. 上题: 1067 Bash游戏 V2 ...

  8. 一、VIP课程:互联网工程专题 02-Git服务搭建与版本分支管理

    第二课:搭建企业私有Git服务.docx 课程概要: GIT远程通信协议详解 基于gogs 搭建WEB管理服务 一.GIT服务器搭建方式 上一节课我们讲过GIT是一个分布式版本管理系统,既然是分布那么 ...

  9. hadoop搭建一:虚拟机网络配置和基础(未完成)

    基于VMware 15+CentOS 7+Hadoop 2.6,hadoop的搭建主要用于个人学习,水平有限. hadoop搭建一:虚拟机网络配置和基础 hadoop搭建二:hadoop全分布搭建 h ...

  10. java使用mongoTemplate去重排序查询

    import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.c ...