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. 百度搜索引擎取真实地址-python代码

    代码 def parseBaidu(keyword, pagenum): keywordsBaseURL = 'https://www.baidu.com/s?wd=' + str(quote(key ...

  2. c# 之 Microsoft.Practices.EnterpriseLibrary连接Oracle

    首先下载Microsoft Enterprise Library 5.0:http://www.microsoft.com/en-us/download/details.aspx?id=15104,这 ...

  3. AtCoder Beginner Contest 120 解题报告

    为啥最近都没有arc啊... A - Favorite Sound #include <algorithm> #include <iostream> #include < ...

  4. BZOJ4893: 项链分赃 && BZOJ4895: 项链分赃(增强版)

    Solution 神仙题.jpg 切一刀简单啊,维护一个前缀和. 切两刀简单啊,拿个队列维护中间那一段. 切三刀,这tm什么毒瘤题. 于是打开题解:"保证不会答案不会超过宝石种类" ...

  5. 如何创建并运行java线程 , 多线程使用

    http://www.importnew.com/20672.html https://www.cnblogs.com/wxd0108/p/5479442.html https://www.cnblo ...

  6. 谷歌大规模机器学习:模型训练、特征工程和算法选择 (32PPT下载)

    本文转自:http://mp.weixin.qq.com/s/Xe3g2OSkE3BpIC2wdt5J-A 谷歌大规模机器学习:模型训练.特征工程和算法选择 (32PPT下载) 2017-01-26  ...

  7. Unity3D学习笔记(三十):Lua

    Lua:是一个轻量级的脚本语句.不需要编译,直接运行的语言.   环境配置:   执行Lua有两种方式: 1.交互式编程(输入后能立即看到效果) 2.脚本式编程:运行lua文件,.lua为文件后缀   ...

  8. 返回Json格式结果

    static string ReturnData(int resultCode, string resultMessage = "", string resultData = &q ...

  9. React入门实例:组件化+react-redux实现网上商城(1)

    项目运行 1.git clone https://github.com/soybeanxiaobi/React_demo_onlineShop 2.cd React_demo_onlineShop(文 ...

  10. Centos6.8安装nginx(一)

    在这里对nginx的安装简单的做个记录,后续有时间的话在详细补充. 1.yum安装g++: yum install -y gcc gcc-c++ [enter] 2.下载必需的依赖库:zlib(为了g ...