Sticks HDU - 1455 (未完成)
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 (未完成)的更多相关文章
- hdu 1455 Sticks
Sticks Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- hdu 1455 Sticks(dfs+剪枝)
题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...
- uva 215 hdu 1455 uvalive5522 poj 1011 sticks
//这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...
- HDU 1455 Sticks(经典剪枝)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 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 ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的 Sample Input95 2 1 5 2 1 5 2 141 2 3 40 Sample Output65 ...
- hdu 1455(DFS+好题+经典)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...
随机推荐
- IDEA引入Gradle工程小记
1.首先IDEA要在该工程Settings中配置本地安装的Gradle,配好其home目录,注意目录到根目录即可,不要到bin一级,否则提示错误,无法使用: 2.配置好后会自动侦测Gradle项目,点 ...
- 论文笔记:Attention Is All You Need
Attention Is All You Need 2018-04-17 10:35:25 Paper:http://papers.nips.cc/paper/7181-attention-is-a ...
- B树,B+树比较
首先注意:B树就是B-树,"-"是个连字符号,不是减号.也就是B-树其实就是B树 B-树是一种平衡的多路查找(又称排序)树,在文件系统中有所应用.主要用作文件的索引.其中的B就表示 ...
- JavaScript 调试常见报错以及原因
JavaScript 调试常见报错以及原因 测试环境 chrome 版本 66.0.3359.170(正式版本) (64 位) TypeError 类型错误 不是操作符所接受的数据类型. //---- ...
- Sublime Text3 插件:DocBlockr与javascript注释规范
原:http://www.ithao123.cn/content-719950.html 1.引子 在写代码的时候,尤其是写脚本,最需要注释了.目前脚本.样式的注释格式都有一个已经成文的约定规范(这些 ...
- 在服务器端对sshd做白名单
1.添加用户 #useradd aaa #passwd aaa -->输入密码:123456 添加3个用户,bbb和ccc与aaa添加一样 2.添加白名单 #vim /etc/ssd/sshd_ ...
- 将tiff文件转化为jpg文件并保存
jar包准备 jai-codec和jai-core 主要过程 private boolean parseTifFile(FileItem item) { logger.info("----- ...
- C#——LINQ语句
委托: //delegate 返回值 委托名(参数); //委托不能在方法中定义 ////实例化委托,并赋值 //委托名 实例名 = new 委托名(函数名).lambda表达式; //使用委托实例, ...
- js实现可视化区域内拖拽
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- git pull 提示 There is no tracking information for the current branch
在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...