NYOJ 293 Sticks 【深搜】+【剪枝】

这是一道让人泪奔的题,它深刻的说明了什么是剪枝,哪怕是再小的一个细节,一旦递归规模增大都会引发巨大的时间消耗,真是神题~

Sticks

时间限制:3000 ms  |  内存限制:65535 KB

难度:5

描述
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return
sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers
greater than zero.

输入
The 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.
输出
The output should contains the smallest possible length of original sticks, one per line.
样例输入
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
样例输出
6
5

#include <cstdio>
#include <algorithm>
using std::sort;
int stick[66], n, sum, len, ok, temp;
bool vis[66];

bool cmp(int a, int b){
	return a > b;
}

bool DFS(int k, int left, int total){ //第k个木棍,剩下的匹配长度,剩下的总长度
	if(left == 0){ //已经匹配完一份
		total -= len;
		if(!total) return true;

		int i = 1;
		while(vis[i]) ++i;
		vis[i] = 1; //cut, 找到的第一根肯定要用,此时就给他用掉,如果不用会TLE
		if(DFS(i + 1, len - stick[i], total)) return true;
		vis[i] = 0;
		return false;
	}

	for(int i = k; i < n; ++i){
		if(i > 0 && stick[i] == stick[i-1] && !vis[i-1]) //cut,若前一个相同长度的匹配失效,则当前不需要深搜下去
			continue;
		if(!vis[i] && left >= stick[i]){
			vis[i] = 1; left -= stick[i];
			if(DFS(i + 1, left, total)) return true;
			vis[i] = 0;
			if(!left) return false; //cut
			left += stick[i];
		}
	}
	return false;
}

int main(){
	while(scanf("%d", &n), n){
		sum = ok = 0;
		for(int i = 0; i < n; ++i){
			scanf("%d", stick + i);
			vis[i] = 0; sum += stick[i];
		}
		sort(stick, stick + n, cmp);
		temp = sum / 2; len = stick[0]; //剪枝1
		vis[0] = 1;
		while(len <= temp){
			if(sum % len == 0 && DFS(1, len - stick[0], sum)){ //剪枝2
				printf("%d\n", len);
				ok = 1; break;
			}
			++len;
		}
		if(!ok) printf("%d\n", sum);
	}
	return 0;
}

NYOJ 293 Sticks 【深搜】+【剪枝】,布布扣,bubuko.com

时间: 2024-08-28 11:24:09

NYOJ 293 Sticks 【深搜】+【剪枝】的相关文章

hdu1455 Sticks 深搜 强剪枝

Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6035    Accepted Submission(s): 1704 Problem Description George took sticks of the same length and cut them randomly until all parts becam

NYOJ 293 Sticks

Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had original

UVA1374 - Power Calculus(迭代深搜+剪枝)

题目链接 题意:给出x和正整数n,问最少需要几次乘除法 可以得到n = x^m 思路:其实是关于指数的操作,即从1到m最少的步数.我们可以先确定最少步数m,然后进行迭代,迭代的过程也就是判断通过相加减所得到的数可以在m次操作中等于n,如果符合,m即为最小步数,如果不符合,m++,进行下一次迭代.迭代过程中要注意剪枝,即剩余的次数如果每次都是取最大值相加还是比n小的话,就直接跳出. 代码: #include <iostream> #include <cstdio> #include

poj1011(深搜+剪枝)

题意:给m根木棍,将它们重新拼成n根一样长的木棍,并使得n尽量大(即每个新木棍尽量短). 解法:经典的搜索题目.从小到大枚举拼成的新木棍长度,每次枚举进行一次深搜.这题关键是如何剪枝. 1.当枚举的长度不能整除总长度的时候,剪枝:(这个很显然) 2.先将木棍从长到短排序,枚举时先尝试长的木棍.(先枚举长的可以使得搜索深度不至于过深) 3.深搜时,不要企图通过换掉一个新木棍的第一根来改变失败的局面(换掉第一根A,那么A也会在以后的新木棍中被使用,但是已经证明了A无法拼成了,所以不必再尝试下去了)

hdu 1518 Square(深搜+剪枝)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! 1 #include <iostream> 2 #include <cstdio> 3 #include<algorithm> 4 #include <cstring> 5 using namespace std; 6 int ap[30],visit[30]

hdu 1455 sticks(经典深搜+剪枝技巧)

 题意:有一堆的木棒,长度不一,它们是有一些整齐的木棒截断而成的,求最小的木棒原始长度. 思路很简单深搜,但是直接深搜的话会tle,首先可以对木棒长度进行排序从大到小,优先使用长度长的木棒,加入当前长度不符合,考虑下一个木棒 其次如果长度为零的时候选择木棒失败,那么直接退出,实测加上这一剪枝就可以ac,这一剪枝可以帮助我们尽可能的在靠近树根处剪枝,所以优化效果很明显. 然后是如果这次选择的木棒长度和上次失败时的一样,那么剪枝. #include<cstdio> #include<cs

hdu1518(Square)深搜+剪枝

点击打开杭电1518 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20,

poj1190 生日蛋糕(深搜+剪枝)

题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到小试 剪枝: ①已建好的面积已经超过目前求得的最优表面积,或者预见到搭完后面积一定会超过目前最优表面积,则停止搭建(最优性剪枝) ②预见到再往上搭,高度已经无法安排,或者半径无法安排,则停止搭建(可行性剪枝) ③还没搭的那些层的体积,一定会超过还缺的体积,则停止搭建(可行性剪枝) ④还没搭的那些层的

ACM 海贼王之伟大航路(深搜剪枝)

"我是要成为海贼王的男人!" 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着"唯一的大秘宝"--ONE PIECE).而航程中间,则是各式各样的岛屿. 因为伟大航路上的气候十分异常,所以来往任意两个岛屿之间的时间差别很大,从A岛到B岛可能需要1天,而从B岛到A岛则可能需要1年.当然,任意两个岛之间的航行时间虽然差别很大,但都是已知的. 现在假设路飞一行从罗格镇(起点)出发,遍历伟大航路中间所有的岛屿(但是已经经过的岛屿不能再次经过),最后到达拉夫德鲁