DFS(剪枝) POJ 1011 Sticks
/*
题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度;
DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全!
剪枝主要在4和5:4 相同长度的木棍不再搜索;5 若新的搜索连第一条都没组合出来,直接break;
详细解释:http://blog.csdn.net/lyy289065406/article/details/6647960
http://www.cnblogs.com/devil-91/archive/2012/08/03/2621787.html
这题虐我千百遍,我待她如。。 656K 0MS 博客随笔第100篇,纪念一下:)
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
int a[MAXN];
bool vis[MAXN];
int sum;
int n, cnt; bool cmp(int x, int y)
{
return x > y;
} bool DFS(int len, int ans, int num, int s)
{
if (num == cnt) return true; int pre = -;
for (int i=s; i<=n; ++i)
{
if (vis[i] || a[i] == pre) continue; //Cut 4 vis[i] = true;
if (ans + a[i] < len)
{
if (DFS (len, ans + a[i], num, i) == true) return true;
else pre = a[i];
}
else if (ans + a[i] == len)
{
if (DFS (len, , num+, ) == true) return true;
else pre = a[i];
}
vis[i] = false; if (ans == ) break; //Cut 5
} return false;
} int main(void) //POJ 1011 Sticks
{
//freopen ("POJ_1011.in", "r", stdin); while (scanf ("%d", &n) == && n)
{
sum = ; memset (vis, , sizeof (vis));
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]); sum += a[i];
}
sort (a+, a++n, cmp); //Cut 1 bool flag = false;
for (int i=a[]; i<=sum-i; ++i) //Cut 2
{
if (sum % i == ) //Cut 3
{
cnt = sum / i;
if (DFS (i, , , ) == true)
{
flag = true;
printf ("%d\n", i); break;
}
}
}
if (!flag) printf ("%d\n", sum);
} return ;
} /*
6
5
*/
DFS(剪枝) POJ 1011 Sticks的更多相关文章
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- POJ 1011 Sticks 【DFS 剪枝】
题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- OpenJudge 2817:木棒 / Poj 1011 Sticks
1.链接地址: http://bailian.openjudge.cn/practice/2817/ http://poj.org/problem?id=1011 2.题目: 总时间限制: 1000m ...
- uva 215 hdu 1455 uvalive5522 poj 1011 sticks
//这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...
- POJ 1011 Sticks(dfs+剪枝)
http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...
- POJ 1011 Sticks dfs,剪枝 难度:2
http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...
- poj(1011)——Sticks(经典的dfs+剪枝)
题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. ...
随机推荐
- Valid Pattern Lock(dfs + 暴力)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
- 解决ie文本框不能输入和获取焦点问题
解决办法: 从正常的机器上拷贝c:\windows\system32\mshtmled.dll到本机的system32目录下即可.或者从安装盘中提取该文件. 加载mshtmled.dll: ...
- Ubuntu上如何安装Java,Eclipse,Pydev,Python(自带,不用装),BeautifulSoup
如何安装Java,如果出于编程的需要安装Java,需要安装的是JDK,而不仅仅是JRE,下面说说如何在Ubuntu下如何安装JDK:只有两步,1.下载并解压,2.配置环境变量1.下载并解压:下载地址: ...
- java中四种阶乘的计算
package com.zf.s2;//创建一个包 import java.math.BigInteger;//导入类 import java.util.ArrayList; import jav ...
- BASH相关
颜色 http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html http://segmentfault.com/q/10100000 ...
- 利用 FFmpeg 和 ImageMagick, AVI 转 GIF(不失真)
利用[TMPGEnc 4.0 XPress] 或 [TMPGEnc Video Mastering Works 5] 生成 AVI 这个视频编辑软件,可对每个帧进行操作 1.生成每个帧的 PNG ff ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 046 Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...
- codeforces 483A. Counterexample 解题报告
题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...
- [编解码] 关于base64编码的原理及实现
转载自: http://www.cnblogs.com/hongru/archive/2012/01/14/2321397.html [Base64]-base64的编码都是按字符串长度,以每3个8b ...