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


相关推荐

  • Java 封装的详解

    Java 封装的详解我们日常使用的电脑主机 把 cpu 内存 主板等等都封装到机箱里面去 假如没有机箱的话的出现什么问题 主机 主板全部都散落在一处 然后开机没有开机按钮 那么需要我们直接操作接跳线才能把电脑开启 这样子的话假如操作不慎的话 会让机器损坏危险 那么假如用机箱封装起来的话 那么就不需要这样子做了 体现了封装的 安全特性 你拿电脑去加内存 可以直接给电脑给维修的人 等他加好内存了之后 你拿到的还是那

    2025年7月1日
    0
  • Linux pstack命令[通俗易懂]

    Linux pstack命令[通俗易懂]概要打印运行进程的栈信息(快照),包括一个进程下的所有线程的栈信息。语法pstackpid解释pstack是封装了gdb功能的shell脚本,通过”threadapplyallbt”的命令获得输出所有的线程堆栈信息,再用sed进行替换和过滤#RunGDB,stripoutunwantednoise.$GDB–quiet$readnever-nx/proc/$1/exe$1<<EOF2>&1|

    2022年9月14日
    0
  • AutoFac文档5(转载)

    AutoFac文档5(转载)

    2021年8月25日
    58
  • 开源 网管 工具_智和网管平台激活成功教程版

    开源 网管 工具_智和网管平台激活成功教程版http://network.51cto.com/art/200906/129490.htm 转载于:https://blog.51cto.com/zhengj3/168618

    2022年10月5日
    1
  • broadcast receiver_consolidator

    broadcast receiver_consolidator应用调用RegisterReciever,实质是调用的ContextImpl的registerReceiver,接下来跟一下这个流程:@OverridepublicIntentregisterReceiver(BroadcastReceiverreceiver,IntentFilterfilter){returnregisterReceiver(receiver,…

    2022年9月12日
    0
  • .net web 开发平台- 表单设计器 一(web版)

    .net web 开发平台- 表单设计器 一(web版)

    2021年12月5日
    36

发表回复

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

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