hdu 1455 Sticks
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
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<algorithm>
using namespace std;
int st[];
bool vis[];
int cnt;
int n;
int l;
bool cmp(int a,int b)
{
return a>=b;
}
bool dfs(int num,int len)
{
if(num==cnt) return true;
for(int i=;i<n;i++)
{
if(!vis[i])
{
if(len+st[i]==l)
{
vis[i]=true;
if(dfs(num+,)) return true;
vis[i]=false; }
if(len+st[i]<l)
{
vis[i]=true;
if(dfs(num,len+st[i])) return true;
vis[i]=false;
if(len==) return ;//这里剪枝很重要,而且很经典。如果当前为0,即本次搜索失败,则一定会废弃一个static,则当前长度的分法肯定不成立。
int last=st[i];
while(last==st[i]) i++;
}
}
}
return false;
}
int main()
{ while((scanf("%d",&n))&&(n!=))
{
for(int i=; i<=n; i++) vis[i]=false;
int sum=;
cnt=;
for(int i=; i<n; i++)
{
scanf("%d",&st[i]);
sum+=st[i];
}
sort(st,st+n,cmp);
for(int i=st[];i<=sum;i++)
{ if(sum%i==)
{
//cout<<"fuck"<<endl;
cnt=sum/i;
l=i;
if(dfs(,))
{
printf("%d\n",l);
break;
}
}
}
}
}
hdu 1455 Sticks的更多相关文章
- hdu 1455 Sticks(dfs+剪枝)
题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...
- HDU 1455 Sticks(经典剪枝)
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- uva 215 hdu 1455 uvalive5522 poj 1011 sticks
//这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...
- Sticks HDU - 1455 (未完成)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- hdoj 1455 Sticks 【dfs】
题意:找最短的木棍可以组成的长度, hdoj 1518 的加强版 代码: #include <stdio.h> #include <string.h> #include &l ...
- hdu Wooden Sticks
这题是暴力加贪心,算是一道水题吧!只要把l和w从小到大排个序就行了... #include"iostream" #include"stdio.h" #inclu ...
- 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 1145(Sticks) DFS剪枝
Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...
随机推荐
- NOIP2011 聪明的质监员
描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[Li ...
- cocos代码研究(1)sprite学习笔记
各种方法创建Sprite和Animate //图片创建法 参数一:图片资源路径 参数二:Rect选区 auto sprite = Sprite::create(, )); addChild(sprit ...
- PHP生成CSV文件
CSV文件的定义这里就不多做介绍了,难能可贵的是用Excel可以直接打开CSV文件.用PHP输出CSV文件本身很简单,但是大家如果有业务需求,下面的代码可以作为参考. $tableheader = a ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- &&队友最近一周水水
100130 练习5 5 hr ago 15.2 days Private qwerqqq 100093 DP2 16 hr ago 50.2 days Private qwerqqq 100092 ...
- 题目1006:ZOJ问题
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:13212 解决:2214 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下:1. ...
- python 异步线程简单实现
import threading def foo(): with open(r'./result.log','wb') as f: f.write('=some logs here ==') t = ...
- SHSEE 备战最后十(四)天日记
努力. Day -1 看书.睡觉. Day 0 上午考试.语文纯RP题跪.理总不错. 下午上课,各种神. Day 1 上午下午讲课...Day 0成绩出来才#17.... Day 2 考试..这次题目 ...
- iptables 命令介绍
http://www.cnblogs.com/wangkangluo1/archive/2012/04/19/2457072.html iptables 防火墙可以用于创建过滤(filter)与NAT ...
- Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. C ...