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


相关推荐

  • AP模式和Router模式区别是什么

    AP模式和Router模式区别是什么AP模式和Router模式有什么区别呢?在TP-Link、水星(Mercury)、迅捷(Fast)等品牌的迷你无线路由器上,有AP模式、Router模式、中继和桥接等几个上网模式,本文重点详细介绍AP模式与Router模式的区别。AP(接入点)模式在AP模式下,需要把迷你无线路由器接入到已经可以上网的路由器或者交换机上面,然后迷你无线路由器可以提供无线WiFi,一般的迷你无线路由器出厂

    2025年8月2日
    3
  • php nginx 负载均衡

    php nginx 负载均衡1:nginx服务器192.168.182.128:8081/代码服务器:192.168.182.129:81192.168.182.131:812:在nginx服务器配置nginx.confupstreamtomcatserver1{server192.168.182.129:81weight=3;server192.168.182.131:81;}server{…

    2022年6月18日
    26
  • generic host process for win32_hostunreachable怎么解决

    generic host process for win32_hostunreachable怎么解决本人使用的windows2003sp1英文版。昨天开始总是莫明其妙出现GenericHostProcess进程出错提示框,紧跟着svchost内存出错提示框, 之后一些service就停止工作,比如WindowsAudio,必须手动重启才能听音乐;网络连接假死,tcp连接需要重连,不胜其扰。上网搜了些文章,基本上分为三个原因:1,木马病毒。2,系统漏洞。3,硬件驱动问题

    2022年10月10日
    4
  • java tess4j ddl,分配为war NoClassDefFoundError后出现Tess4J错误:无法初始化类net.sourceforge.tess4j.TessAPI…「建议收藏」

    java tess4j ddl,分配为war NoClassDefFoundError后出现Tess4J错误:无法初始化类net.sourceforge.tess4j.TessAPI…「建议收藏」IhaveSpringbootWebserverprojectwhichworksokinmyPCunderIntellijIDEA,butitnotworksafterdistributingtothesamePCaswarfile-NoClassDefFoundError:Couldnotinitializeclassnet…

    2022年5月10日
    40
  • c++ 11 list转set「建议收藏」

    c++ 11 list转set「建议收藏」list<int> li; for(inti=0;i<100;i++){ li.push_back(i); } for(inti=0;i<100;i++){ li.push_back(i); } unordered_set<int> uset(li.begin(),li.end());//用list去初始化s…

    2022年6月21日
    49
  • Lucene分词报错:”TokenStream contract violation: close() call missing”

    Lucene分词报错:”TokenStream contract violation: close() call missing”Lucene使用IKAnalyzer分词时报错:”TokenStreamcontractviolation:close()callmissing”解决办法是每次完成后必须调用关闭方法。如果报错:java.lang.illegalstateexception:tokenstreamcontractviolation:reset()/close()callmissing,…

    2022年7月22日
    18

发表回复

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

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