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. 
InputThe 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. 
OutputThe 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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; int n,cnt,sum;//设置全局变量,n表示木棍的数量,sum表示n个木棍的总长度,cnt=sum/i; struct node
{
int lenth; // 木棍的长度
int mark; //标记这根木棍是否被用过;
}stick[]; int cmp(node a,node b) //按长度从大到小排序
{
return a.lenth>b.lenth;
} //len为当前木棒的长度,count统计当前长度木棒的数量,
int dfs(int len,int count,int l,int pos)
{
if(len==sum) return ; //递归出口
if(count==cnt) return ; for(int i=pos;i<n;i++)
{
if(stick[i].mark)continue; //如果木棍被标记过,跳过去
if(len==(stick[i].lenth+l))
{
stick[i].mark=;
if(dfs(len,count+,,)) return ;
stick[i].mark=;
return ;
}
else if(len>(stick[i].lenth+l))
{
stick[i].mark=;
l+=stick[i].lenth;
if(dfs(len,count,l,i+))
return ;
l-=stick[i].lenth; //如果不能拼接,那么就恢复
stick[i].mark=;
if(l==) return ;
while(stick[i].lenth==stick[i+].lenth)i++; //如果不剪支跑一遍要140MS,加上剪支跑一遍只要31MS,ORZ
}
}
return ;
} int main()
{
while(cin>>n&&n)
{
cnt=sum=;
for(int i=;i<n;i++)
{
cin>>stick[i].lenth;
sum+=stick[i].lenth;
stick[i].mark=;
}
sort(stick,stick+n,cmp);
for(int i=stick[].lenth;i<=sum;i++)
{
if(sum%i)continue;
cnt=sum/i;
if(dfs(i,,,))
{
printf("%d\n",i);
break;
}
} }
return ;
}

Sticks HDU - 1455 (未完成)的更多相关文章

  1. hdu 1455 Sticks

    Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

  3. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

  4. HDU 1455 Sticks(经典剪枝)

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

  5. 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 ...

  6. POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

    参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...

  7. hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)

    N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的 Sample Input95 2 1 5 2 1 5 2 141 2 3 40 Sample Output65 ...

  8. hdu 1455(DFS+好题+经典)

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

  9. HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...

随机推荐

  1. 使用vs code搭建C开发环境

    关于搭建vscode的开发环境来开发c网上已经有很多类似的贴子,但是几乎都是直接给出tasks.json和launch.json文件,并未说明这两个文件的作用以及如何配置.这里面向纯小白着重说明下(1 ...

  2. 【示例】Spring Quartz入门

    JAVA 针对定时任务,有 Timer,Scheduler, Quartz 等几种实现方式,其中最常用的应该就是 Quartz 了. 一. Quartz的基本概念 在开始之前,我们必须了解以下的几个基 ...

  3. 好用的js模板

    组织form下的 json对象 $.fn.serializeObject = function() { var o = {"unique_id":new Date().getTim ...

  4. 网络通信 & 初识socket

    本节主要内容: 1.客户短\服务端架构 2.网络通信的流程 3.初识socket 一.客户端\服务端架构 客户端\服务端架构: 即Client/Server (C/S) 结构,是大家熟知的软件系统体系 ...

  5. mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

    工具及背景: IntelliJ IDEA 2016.1.3 Ultimate.spring boot, maven项目,利用mybatis 注解的方式查询mysql. 业务逻辑关系:controlle ...

  6. mark mem

    韦达定理 http://baike.baidu.com/link?url=M45ozZEnQ4BtKD7l22WWgQuGnmDYV7TFynQcPEO2Tt8leYGhyEa1flt-RM34NG4 ...

  7. 【转】Windows下selenium+python自动化测试环境搭建

    原文链接:http://www.cnblogs.com/test-of-philosophy/articles/4322918.html 搭建平台:windows 1.安装python,下载地址:ht ...

  8. Oracle(限定查询1)

    3.1.认识限定查询 例如:如果一张表中有100w条数据,一旦执行了“SELECT * FROM 表”语句之后,则将在屏幕上显示表中的全部数据行的记录,这样即不方便浏览,也有可能造成死机的问题出现,所 ...

  9. String和常量池

    1.Java 会确保一个字符串常量只有一个拷贝 2.用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间 ...

  10. Eclispe中编辑xml配置文件时不会提示也不能自动调整格式

    创建了一个xml文件后,发现编辑起来和原来的那些有所不同,不会提示补全.也不能自动调整格式???woc? 哈哈哈哈哈,“我最恨你像个石头一样” 后来发现是编辑器被改了!!! 右键xml文件然后open ...