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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • TCP标志:PSH和URG

    TCP标志:PSH和URGTCP 标头包含几个一位布尔字段 称为标志 用于影响 TCP 连接上的数据流 忽略 RFC3168 为拥塞通知添加的 CWR 和 ECE 标志 有六个 TCP 控制标志 下面列出的其中四个用于控制 TCP 连接的建立 维护和拆除 并且对于甚至进行了基本数据包分析的任何人都应该熟悉 SYN nbsp 启动连接 ACK nbsp 确认收到的数据 FIN nbsp 关闭连接 RST nbsp 中止连接以响应错误其他两个标志 PSH 推 和

    2025年10月17日
    3
  • python下载ts视频文件「建议收藏」

    python下载ts视频文件「建议收藏」importrequestsfrommultiprocessingimportPooldefmission(url,n):headers={"User-Agent":"Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/69.0.3497.100Safa…

    2022年7月18日
    59
  • 关于android studio 中安装intel haxm问题的解决

    关于android studio 中安装intel haxm问题的解决关于androidstudio安装intelhaxm问题的解决遇到的问题解决问题总结遇到的问题安装androidstudio过程中intelhaxm失败,导致后续笔记本运行模拟器过程中漫长等待让我痛不欲生。于是着手解决intelhaxm安装失败问题。我的笔记本型号是thinkpadw510,处理器i7Q720,操作系统windows7sp1。解决问题1、进入andro…

    2022年6月28日
    40
  • ioctl函数_通过ioctl函数设置IP不允许修改

    ioctl函数_通过ioctl函数设置IP不允许修改一、什么是ioctl  ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。  ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。  用户程序所作的只是通过命令码(cmd)告诉驱动程序它想…

    2022年10月18日
    3
  • Hadoop生态系统图

    Hadoop生态系统图当下Hadoop已经成长为一个庞大的生态体系,只要和海量数据相关的领域,都有Hadoop的身影。下图是一个Hadoop生态系统的图谱,详细列举了在Hadoop这个生态系统中出现的各种数据工具。这一切,都起源自Web数据爆炸时代的来临。Hadoop生态系统的功能以及对应的开源工具说明如下。MapReduce

    2022年5月19日
    50
  • SpringBoot线程池使用详解

    SpringBoot线程池使用详解前提摘要:基于Springboot2.1.4.RELEASE▎配置TaskExecutorimportjava.util.concurrent.ThreadPoolExecutor;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.ta…

    2022年6月22日
    38

发表回复

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

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