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 ...
随机推荐
- 彻底地/ 终于地, 解决 关于apache 权限的问题了:: 修改 DocumentRoot后的 403错误: have no permission to access / on this server
目录的权限都 应该设置 为 drwxr_xr_x, 即755, 而html下的文件的权限设置为; 644 即可! -x 只有目标文件对某些用户是可执行的或该目标文件是目录时才追加x 属性. -w权限, ...
- CF767C 记录错误
链接 https://codeforces.com/contest/767/problem/C 思路 之所以把这个题放进来,是因为要记录错误 情况不止一种 所以答案存储就是>=2了 代码 #in ...
- P4137 Rmq Problem / mex
目录 链接 思路 线段树 莫队 链接 https://www.luogu.org/problemnew/show/P4137 思路 做了好几次,每次都得想一会,再记录一下 可持久化权值线段树 区间出现 ...
- 论文笔记: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 ...
- (转载)MySQL用命令行复制表的方法
mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 ; 或 CREATE TABLE 新表 LIKE 旧表 ; 注意上面两种方式,前一种方式是不会复制时的主键类型和自增方式是 ...
- Sublime text 3 汉化教程
首先,需要安装Package Control 启动并进入sublime主界面,打开Sublime Text的控制台(快捷键 ctrl + ~) 然后我们到Package Control官方网站,复制s ...
- Latex: 添加IEEE论文keywords
参考: How to use \IEEEkeywords Latex: 添加IEEE论文keywords 方法: \begin{IEEEkeywords} keyword1, keyword2. \e ...
- ashx图片上传接收
发送数据流方法 /// <summary> /// PostBinaryData /// </summary> /// <param name="url&quo ...
- pl/sql编译存储过程卡住的解决方法
oracle编译存过卡住处理: 问题描述: 在编译某个存过时,由于没提交或断网或者test没停止又重新编译,导致编译存过一直卡死 问题分析: 存过或某张表被锁 问题处理: 1.查看存过是否锁住,loc ...
- 将矩阵数据转换为栅格图 filled.contour()
require(grDevices) # for colours filled.contour(volcano, color = terrain.colors, asp = 1) # simple x ...