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. ubuntu下操作端口的方法

    最简单的一个操作:sudo ufw status可检查防火墙的状态,我的返回的是:不活动 sudo ufw version防火墙版本: ufw 0.29-4ubuntu1 Copyright 2008 ...

  2. 【翻译自mos文章】在重建控制文件之前应该考虑的事情

    在重建控制文件之前应该考虑的事情 来源于: Things to Consider Before Recreating the Controlfile (文档 ID 1475632.1) 适用于: Or ...

  3. scikit-learn(project中用的相对较多的模型介绍):1.14. Semi-Supervised

    參考:http://scikit-learn.org/stable/modules/label_propagation.html The semi-supervised estimators insk ...

  4. Java 迭代器

    1.快速报错 public class FailFast { public static void main(String[] args) { List<String> c = new A ...

  5. Android Dev Tips

    Ref:Android Service与Activity之间通信的几种方式 Ref:基础总结篇之五:BroadcastReceiver应用详解 Ref:Android Activity和Intent机 ...

  6. (转)Javascript模块化编程(一):模块的写法

    随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个团队分工协作.进度管理.单元测试等等......开发者 ...

  7. Redis持久化——AOF(二)

    核心知识点: 1.AOF:以独立日志的方式记录写命令,重启时再执行命令.与RDB不同的是解决数据持久化的实时性,可以记录所有写操作. 2.AOF工作流程:写入命令.文件同步.文件重写.文件加载. 3. ...

  8. iOS UIImage 图片局部拉伸的一些学习要点

    之前 做纯色局部拉伸 通过 top  bottom left  right 相交的阴影拉伸 屡试不爽 实施方法: imageView.image = [[UIImage imageNamed: @&q ...

  9. Python 3 并发编程多进程之守护进程

    Python 3 并发编程多进程之守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemo ...

  10. 前端开发笔记--flex布局

    flex布局: 个人觉得flex布局比起传统布局要优先得多(主要是容易使用),缺点是IE10及以上版本才能使用,甚至某些属性只有在IE11才能使用(而且我发现凡是不兼容主要IE的坑来的多,不是说其他浏 ...