poj 1011 Sticks (DFS+剪枝)

poj 1011 Sticks (DFS+剪枝)

大家好,又见面了,我是全栈君。

Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 127771   Accepted: 29926

Description

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.

Input

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.

Output

The output should contains the smallest possible length of original sticks, one per line.

Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

Source

题目链接:http://poj.org/problem?id=1011


题目大意:有n根木棍。用它们拼成一些等长的木棍,求拼出的木棍的最短长度。


解题思路:这题的时间限制特别严格。

DFS+剪枝,剪枝较多。首先由多到少枚举木棍数目num。即从n到1,要满足木棍总长度是num的倍数,且拼出的长度要不小于最长的木棍长度,否则无法拼,搜索到答案后退出循环,保证求出的木棍长最短。

剪枝:1.木棍由长到短排序。

   2.訪问过的木棍或者加上当前木棍长度后超过了目标长度,则跳过本次循环。

   3.若当前木棍和上一根木棍长度同样而且上一根木棍没用到,则跳过本次循环。

   4.dfs中标记開始木棍下标。


代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[66],vis[66];
int n,num,m;
bool p;
int cmp(int a,int b)
{
	return a>b;
}
void dfs(int st,int cur,int cnt)
{
	if(p||cnt==num)
	{
		p=true;
		return ;
	}
	for(int i=st;i<n;i++)
	{
		if(vis[i]||cur+a[i]>m)    //訪问过的木棍或者加上当前木棍长度后超过了目标长度,则跳过本次循环
			continue;
		if(i-1&&!vis[i-1]&&a[i]==a[i-1])    //若当前木棍和上一根木棍长度同样而且上一根木棍没用到,则跳过本次循环。
			continue;
		if(a[i]+cur==m)
		{
			vis[i]=1;
			dfs(0,0,cnt+1);
			vis[i]=0;
			return;				//循环里后面的值都在dfs中求过了。这里直接返回上一层
		}
		if(a[i]+cur<m)
		{
			vis[i]=1;
			dfs(i+1,a[i]+cur,cnt);
			vis[i]=0;
			if(cur==0)           //cur为0时,直接返回上一层
				return ;
		}
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		int sum=0;
		p=false;
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		sort(a,a+n,cmp);
		for(num=n;num>=1;num--)
		{
			if(sum%num||a[0]>sum/num)
				continue;
			m=sum/num;
			dfs(0,0,0);
			if(p)
				break;
		}
		printf("%d\n",m);
	}
	return 0;
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115729.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 微机原理考试试题及答案分析_微机原理及接口技术

    微机原理考试试题及答案分析_微机原理及接口技术微机原理与接口技术期末考试试题及答案微机原理与接口技术期末考试题库微机系统的硬件由哪几部分组成?答:三部分:微型计算机(微处理器,存储器,I/0接口,系统总线),外围设备,电源。什么是微机的总线,分为哪三组?答:是传递信息的一组公用导线。分三组:地址总线,数据总线,控制总线。8086/8088CPU的内部结构分为哪两大模块,各自的主要功能是什么?答:总线接口部件(BIU)功能:根据执行单元EU的…

    2022年9月1日
  • Minimum Fleet Problem「建议收藏」

    Minimum Fleet Problem「建议收藏」本文为MITSenseableCityLaboratory2018年5月23号发表于Nature杂志Addressingtheminimumfleetprobleminon-demandurbanmobility论文的学习笔记。问题定义给定一批出行需求,在出行需求被严格满足且最大空驶时间不超过δ分钟约束下,找到…

    2022年6月5日
  • mysql autoconf_autoconf手册(一)

    mysql autoconf_autoconf手册(一)AutoconfCreatingAutomaticConfigurationScriptsEdition2.13,forAutoconfversion2.13December1998byDavidMacKenzieandBenElliston—————————————————————–…

    2022年6月4日
  • PyCharm 教程(五)断点 调试[通俗易懂]

    PyCharm 教程(五)断点 调试[通俗易懂]PyCharm作为IDE,断点调试是必须有的功能。否则,我们还真不如用纯编辑器写的快。【运行】和【调试】前的设置,详见前面的文章,helloword。1,设置断点在代码前面,行号的后面,鼠标单击,就可以设置断点。如下:2,调试断点点击那个绿色的甲虫图标(似乎甲虫已经成为debug专用图标了),进行断点调试。点击后,会运行到第一个断

    2022年5月21日
  • hdu 2838 Cow Sorting(树状数组)

    hdu 2838 Cow Sorting(树状数组)

    2022年1月21日
  • POJ 2208 已知边四面体六个长度,计算体积

    POJ 2208 已知边四面体六个长度,计算体积

    2022年1月6日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号