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 ...
随机推荐
- TV Show Game 【2-SAT】
问题 K: TV Show Game 时间限制: 1 Sec 内存限制: 512 MB Special Judge 提交: 51 解决: 10 [提交] [状态] [命题人:admin] 题目描 ...
- HihoCoder 1636 Pangu and Stones(区间DP)题解
题意:合并石子,每次只能合并l~r堆成1堆,代价是新石堆石子个数,问最后能不能合成1堆,不能输出0,能输出最小代价 思路:dp[l][r][t]表示把l到r的石堆合并成t需要的最小代价. 当t == ...
- Custom Quality Profiles in SonarQube
https://medium.com/ltunes/custom-quality-profiles-in-sonarqube-part-1-8754348b9369 Creating Custom Q ...
- P2051 [AHOI2009]中国象棋(动态规划)
思路 好像是一道挺水的计数的,不知道为什么会是紫题 显然每行和每列最多放两个 首先考虑状压,然后发现三进制状压可做,但是三进制太麻烦了,可以拆成两个二进制,一个表示该列是否是放了一个的,一个表示该列是 ...
- node.js模块本地代理模块(将自己本机/局域网)服务 代理到外网可以访问
npm 模块 localtunnel模块可以. lt --port 7000 其中7000是本地服务端口 会分配一个动态的url. 局域网中查看本地ip用ifconfg 或者在系统偏好设置里面查看网 ...
- win10常用命令和设置总结
1.常用命令 exit:退出cmd面板; cls:清除cmd面板; 2.常用设置 2.1 services.msc 禁用:以后怎样都不会运行;手动:是打开某些用到它的程序要用到该服务时才会运行; 自动 ...
- 导入dmp文件时,需要删除原有ORACLE数据库实例
导入dmp文件时,对于已存在的数据库实例及表处理方式:删除实例. 1.以管理员身份登录 sqlplus / as sysdba 2.停止实例 shutdown abort; 执行结果:ORACLE i ...
- Eclipse+maven 构建第一个简单的springmvc项目
先给出项目的目录: 在eclipse下使用maven构建第一个springmvc项目步骤如下: 1.创建maven project(此处默认你已了解maven),此处需要注意以下两点 2.创建完毕后会 ...
- spring容器bean的作用域 & spring容器是否是单例的一些问题
Spring容器中Bean的作用域 当通过Spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...
- learn python the hard way 习题1~5总结
习题1 print 语句print('Yay! Printing.')print('I "said" do not touch this') 习题2:注释和 # 号 #(octot ...