Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8839    Accepted Submission(s): 2623

Problem Description
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. 
 
Input
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.
 
Output
The output file contains the smallest possible length of original sticks, one per line. 
 
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
 
Sample Output
6
5
思路:做本题之前先做HDU1518,dfs+剪枝。见代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
int n;
int sticks[MAXN];
int sum,l;
int vis[MAXN];
bool comp(int a,int b)
{
return a > b;
}
bool dfs(int dep,int len,int start)//dep:搜索深度,len:sticks连接的长度,start:当前搜索的木棒
{
if(dep==n)
{
if(len==)
return true;
else
return false;
}
for(int i=start;i<n;i++)
{
if(!vis[i]&&len+sticks[i]<=l)
{
vis[i]=;
if(len+sticks[i]<l)
{
if(dfs(dep+,len+sticks[i],i+))
return true;
}
else
{
if(dfs(dep+,,))
return true;
}
vis[i]=;
if(len==) return false;//如果sticks[i]作为木棒的头却没有匹配成功的话,
//说明sticks[i]不能与其他stick连接,换l从新搜
while(sticks[i+]==sticks[i]) i++;//若本次sticks不可以,则与它等长的也不可以
}
}
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
sum=;
for(int i=;i<n;i++)
{
scanf("%d",&sticks[i]);
sum+=sticks[i];
}
sort(sticks,sticks+n,comp);//必须由大到小排序
for(l=sticks[];l<=sum;l++)
{
if((sum%l)!=) continue;//源sticks是等长的
memset(vis,,sizeof(vis));
if(dfs(,,))
{
printf("%d\n",l);
break;
}
}
}
return ;
}
 

HUD1455:Sticks的更多相关文章

  1. HDOJ 1051. Wooden Sticks 贪心 结构体排序

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

  2. POJ 2653 Pick-up sticks (线段相交)

    题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...

  3. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  4. POJ 2653 Pick-up sticks【线段相交】

    题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include ...

  5. POJ1065Wooden Sticks[DP LIS]

    Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21902   Accepted: 9353 De ...

  6. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

  7. CF451A Game With Sticks 水题

    Codeforces Round #258 (Div. 2) Game With Sticks A. Game With Sticks time limit per test 1 second mem ...

  8. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  9. The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543

    Pick The Sticks Time Limit: 15000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

随机推荐

  1. Xshell连接不上行Linux系统

    之前出现了.我的Linux系统上的主机可以ping通windows的一台电脑, windows也可以ping的通Linux.但是Xshell不能连接. 第一,我感觉是不是我Linux系统的端口被防火墙 ...

  2. python脚本分析nginx访问日志

    日志格式如下: 223.74.135.248 [11/May/2017:11:19:47 +0800] "POST /login/getValidateCode HTTP/1.1" ...

  3. 在VS2017环境中编译libxml2库

    libxml2库编译 1.下载libxml2,官网是:http://www.xmlsoft.org/downloads.html, 我下载的版本是:libxml2-sources-2.9.7.tar. ...

  4. 18.Django原生SQL语句查询返回字典

    在django中执行自定义语句的时候,返回的结果是一个tuple ,并我不是我所期望的dict.当结果是tuple 时,如果要取得数据,必须知道对应数据在结果集中的序号,用序号的方式去得到值. 如果是 ...

  5. 财经世界(5)国际货币基金组织,世界银行,国际清算银行(BIS)与美联储

    (1)国际货币基金组织 *国际货币基金组织(International Monetary Fund,简称:IMF)是根据1944年7月在布雷顿森林会议签订的<国际货币基金协定>,于1945 ...

  6. windows8.1下安装msi文件报错

    新安装了win8.1系统体验体验,可是安装msi文件的软件报internal error2502和2503错误,可以换一种安装方式. 不是直接点开安装,如图所示打开命令提示符: 使用msiexec / ...

  7. JFreeChart的使用示例

    示例一,饼图,简单示例: 导入jar,代码文件: 运行结果: 代码: import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartF ...

  8. Docker-使用Dockerfile创建镜像

    Dockerfile是一个文本格式的配置文件,用户可以使用Docker来快速创建自定义的镜像 基本结构 Dockerfile由一行行命令语句组成,并且支持以#开头的注释行 一般而言,Dockerfil ...

  9. JSP&EL 内置对象

    JSP&EL 内置对象 转载▼   具体的JSP和El中的内置对象见下表,由于我写在了excel中,也不知道怎么把excel发出来,就截了图. 相关问题: Q1: JSP:EL中 pageCo ...

  10. UOJ78 二分图最大匹配

    从前一个和谐的班级,有 nlnl 个是男生,有 nrnr 个是女生.编号分别为 1,…,nl1,…,nl 和 1,…,nr1,…,nr. 有若干个这样的条件:第 vv 个男生和第 uu 个女生愿意结为 ...