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> ...
随机推荐
- IdeaVim-常用操作
IdeaVim简介 IdeaVim是IntelliJ IDEA的一款插件,他提高了我们写代码的速度,对代码的跳转,查找也很友好. 安装位置 安装之后它在 Tools > Vim Emulator ...
- ThinkPHP 3.2 用户注册邮箱验证帐号找回密码
一.前言 当然现在有的网站也有手机短信的方式找回密码,原理就是通过发送验证码来验明正身,和发送邮件验证一样,最终还是要通过重置密码来完成找回密码的流程. 本文将使用PHP+Mysql+jQuery来实 ...
- [iOS]App上架流程[利用Archive进行上传]
今天给大家带来项目如何上架的教程 准备: 1. 一个开发者账号(需要交过钱的,
- Keil4 几例异常解决办法
以下几个问题在Keil5上没有发现,不过因为下载的例子都是Keil4,强迫症让我用起了Keil4 错误一:main.c: Warning: C3910W: Old syntax, please use ...
- POJ3304 Segments 【线段直线相交】
题意: 给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!. 思路: 计算几何.这道题要思考到两点: 1:把问题转化为是否存在一条直线 ...
- Apple Watch 开发详解
Apple Watch 开发详解 Apple Watch现在对于第三方开发者来说更多的还是一块额外的屏幕.暂时WatchKit没有能给出足够的接口.现在Watch App的主要运算逻辑需要依赖iPho ...
- aircrack-ng笔记
开启监听: airmon-ng start wlan0 抓包: airodump-ng wlan0mon 查看wifi ^C结束 airodump-ng -c 6 --bssid C8:3A:35:3 ...
- <crtdbg.h> 的作用
1.在调试状态下让win程在输出窗口中显示调试信息,可以用_RPTn 宏n为显示参数比如_RPT0(_CRT_WARN,"text"); _RPT1(_CRT_WARN," ...
- SpringBoot整合Email(电子邮件服务)
(1).导入starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- Mac下的开发工具
1.webstrom 淘宝上2块钱就能买一个 WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑 ...