hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
# include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int sum , num;
int a[] ;
bool v[] ;
int len , n; bool cmp(const int &x , const int &y)
{
return x > y ;
} bool dfs(int count , int L , int pos) //已完成的数量 当前木棒长度 位置
{
if (len == sum)
return ;
if (count == num)
return ;
for (int i = pos ; i < n ; i++)
{
if (v[i])
continue ;
if (L + a[i] == len)
{
v[i] = ;
if (dfs(count+ , , ))
return ;
v[i] = ;
return ;
}
else if (L + a[i] < len)
{
v[i] = ;
if (dfs(count , L+a[i] , i+))
return ;
v[i] = ;
if (L == ) //说明有一根木棒没有派上用场
return ;
while (a[i] == a[i+]) //如果下一根的长度和这根一样 则继续搜索下面的
i++ ;
}
}
return ;
} int main()
{
//freopen("in.txt","r",stdin) ;
while (scanf("%d" , &n) , n)
{
sum = ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d" , &a[i]) ;
sum += a[i] ;
}
sort(a,a+n,cmp) ;
if (a[] > sum - a[]) //如过第1根木棒比剩下的木棒和 还要长
{
printf("%d\n" , sum) ;
continue ;
}
for (len = a[] ; len <= sum ; len++) //一根完整木棒的长度肯定大于最长的小木棒
{
if (sum % len)
continue ;
memset(v , , sizeof(v)) ;
num = sum/len ;
if (dfs(,,))
{
printf("%d\n" , len) ;
break ;
}
}
}
return ; }
hdu 1518 N根木棒 能否拼成正方形
Sample Input
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
Sample Output
yes
no
yes
# include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int sum ;
int a[] ;
bool v[] ;
int len , n; bool cmp(const int &x , const int &y)
{
return x > y ;
} bool dfs(int count , int L , int pos) //已完成的数量 当前木棒长度 位置
{ if (count == )
return ;
for (int i = pos ; i < n ; i++)
{
if (v[i])
continue ;
if (L + a[i] == len)
{
v[i] = ;
if (dfs(count+ , , ))
return ;
v[i] = ;
return ;
}
else if (L + a[i] < len)
{
v[i] = ;
if (dfs(count , L+a[i] , i+))
return ;
v[i] = ;
if (L == ) //说明有一根木棒没有派上用场
return ;
while (a[i] == a[i+]) //如果下一根的长度和这根一样 则继续搜索下面的
i++ ;
}
}
return ;
} int main()
{
// freopen("in.txt","r"a,stdin) ;
int T ;
scanf("%d" , &T) ;
while (T--)
{
scanf("%d" , &n) ;
sum = ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d" , &a[i]) ;
sum += a[i] ;
}
if (sum%)
{
printf("no\n") ;
continue ;
}
sort(a,a+n,cmp) ;
len = sum / ;
if (a[] > len)
{
printf("no\n") ;
continue ;
}
memset(v,,sizeof(v)) ;
if (dfs(,,))
{
printf("yes\n") ;
}
else
printf("no\n") ; }
return ; }
hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)的更多相关文章
- hdu 1455 Sticks
Sticks Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 1455 Sticks(dfs+剪枝)
题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...
- uva 215 hdu 1455 uvalive5522 poj 1011 sticks
//这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...
- 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 ...
- Sticks HDU - 1455 (未完成)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- HDU 1455 Sticks(经典剪枝)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Hdu 1455
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...
随机推荐
- 图解项目管理流程:禅道&JIRA中的操作
禅道作为优秀的国产开源项目管理软件,禅道集产品管理.项目管理.质量管理.文档管理.组织管理和事务管理于一体,完整覆盖了研发项目管理的核心流程.禅道的设计理念在于提供一体化的解决方案,流程图完整呈现了项 ...
- minio上传大于30M文件失败
minio上传30M文件失败.提示失败或者进度不动.翻查手册发现并未限速.原因是使用了nginx做代理.上传超过30M大的客户端文件无法正常上传,修改了下nginx的配置,就可以了. 加上client ...
- Elasticsearch之中文分词器插件es-ik的自定义词库
它在哪里呢? 非常重要! [hadoop@HadoopMaster custom]$ pwd/home/hadoop/app/elasticsearch-2.4.3/plugins/ik/config ...
- luogu 4158 粉刷匠 dp套dp
dp套dp 每个木板是个递推的dp,外部是个分组背包 #include<bits/stdc++.h> #define rep(i,x,y) for(register int i=x;i&l ...
- C# 部分类使用partial修饰
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace pati ...
- ==========2014-04-24=========winform树控件勾选方法 和获取所有选中的
http://bbs.bccn.net/thread-197567-1-1.html /// <summary> /// 已选中或取消选中树节点上的复选框时 /// </summar ...
- NOIP2018TG题解
应该不会被禁赛吧qwq Day1 T1 铺设道路 €€£:我 抄 我 自 己 我考场上写的是一个类似于差分的做法,就是开个数组表示某个位置是否贡献答案,从小到大枚举高度,那么一个位置能贡献答案当且仅当 ...
- dbeaver can't connect HBase1.2 using phoenix driver #1863
1 第一个问题 Unexpected version format: 10.0.2 Unexpected version format: 10.0.2 Unexpected version forma ...
- delphi-TTcpServer与TTcpClient
最简单的TTcpServer与TTcpClient通信实例-Delphi_海盗船长_新浪博客http://blog.sina.com.cn/s/blog_5383794d0100nt9u.html d ...
- Docker容器命令
★根本前提:本地主机有镜像才能创建容器 ⒈docker run [Options] 镜像名称或镜像ID [Command] [Arg...] 用途:利用镜像创建容器实例 Options说明(常用):注 ...