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. x-www-form-urlencoded名字的由来

    1 提交的是表单数据 所以用form. 2 提交的形式是以参数放在url后面的形式提交的 例如,以x1=y1&x2=y2&x3=y3的形式放在url后面的形式提交,所以是urlenco ...

  2. HTML/CSS/JS初始化

    CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...

  3. zip filter map 列表生成器

    map map(function, list): 就是对list 中的每一个元素都调用function函数进行处理,返回一个map的对象 list一下就可以生成一个列表 或者for循环该对象就可以输出 ...

  4. 我的Android进阶之旅------>WindowManager.LayoutParams介绍

    本文转载于: http://hubingforever.blog.163.com/blog/static/171040579201175111031938/ 本文参照自: http://develop ...

  5. Android N API预览

    Android N for Developers 重要的开发人员功能 多窗体支持 通知 JIT/AOT 编译 高速的应用安装路径 外出瞌睡模式 后台优化 Data Saver 高速设置图块 API 号 ...

  6. Python:笔记(1)——基础语法

    Python:笔记(1)——基础语法 我很抱歉有半年没有在博客园写过笔记了,客观因素有一些,但主观原因居多,再多的谴责和批判也都于事无补,我们能做的就是重振旗鼓,继续出发! ——写在Python之前 ...

  7. Java进阶学习:log4j的学习和使用

    Java进阶学习——log4j的学习和使用 简介Loj4j Log4j的组成 Log4j主要由三大组组件构成: Logger: 负责生成日志,并能够对日志信息进行分类筛选,通俗的讲就是决定什么日志信息 ...

  8. Data Structure Array: Sort elements by frequency

    http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...

  9. [GUI] QT事件与X11的关系

    做了一段时间linux下与QT事件相关的工作,经常会遇到X11,总是苦于无法完全理解其与linux以及QT事件之间的关系,所以用两篇文章来简单总结下linux中的图形管理和QT事件与X11的关系. & ...

  10. 0523 CSS知识点

    高级选择器分为:后代选择器.子代选择器.并集选择器.交集选择器 后代选择器 使用空格表示后代选择器.顾名思义,父元素的后代(包括儿子,孙子,重孙子) .father .item .a p{color: ...