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)
上一篇 2022年1月31日 下午1:00
下一篇 2022年1月31日 下午1:00


相关推荐

  • Nginx 负载均衡演示之 upstream 参数 & location 参数

    Nginx 负载均衡演示之 upstream 参数 & location 参数nbsp upstream 参数 nginx 关于 upstream 参数官方文档 http nginx org en docs http ngx http upstream module htmlupstream 参数 参数 描述 service 反向服务地址加端口 weight 权

    2026年3月20日
    2
  • MySQL的JDBC连接

    MySQL的JDBC连接MySQL的JDBC连接MySQL的JDBC概念MySQL的JDBCJDBC添加数据封装连接工具更新数据和事务删除数据查询数据MySQL的JDBC概念JDBC是JavaDatabaseConnective的缩写,表示使用Java去连接数据库进行数据操作的过程MySQL的JDBC创建动态项目-以eclipse为例,首先要创建动态项目连接开发包(在www.mvnrepositor…

    2022年7月17日
    15
  • 【zabix笔记】折线图展示平均值、最大值与最小值

    【zabix笔记】折线图展示平均值、最大值与最小值上周看到 zabix 显示 CPU 使用时间指标 利用三条不同颜色的折线叠加显示了平均值 最大值与最小值 以及工作日 非工作日几项信息 非常受启发 在上图中 深绿色的线显示平均值 浅绿色和深粉色的线分别显示最小值和最大值 白色区域为工作时间 工作日 灰色区域为非工作时间 zabix 关于该图的解释这种类型的折线图可以应用到很多场景 比如 工单的处理时间 平均处理时间 最长时间 最短时间 订单价格

    2026年3月16日
    2
  • Windows下如何配置tomcat环境变量

    Windows下如何配置tomcat环境变量pacheTomcat 是一款 JavaServlet 和 JavaServerPa 技术的开源软件实现 可以作为测试 Servlet 的独立服务器 而且可以集成到 ApacheWeb 服务器 下面是在电脑上安装 Tomcat 的步骤 一 工具 在配置 tomcat 之前需要配置好 JDK 的环境 JDK 配置可以参照博文 Windows 环境下 JDK 安装与环境变量配置 Tomcat 安装包 我这里选择的是 Tomcat7 apache tomcat 7 0 57 windows x

    2026年3月16日
    2
  • MySQL JDBC URL各参数详解

    MySQL JDBC URL各参数详解参数名称参数说明缺省值最低版本要求user数据库用户名(用于连接数据库)password用户密码(用于连接数据库)useUnicode是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为truefalse1.1guseSSLMySQL在高版本需要指明是否进行SSL连接在mysql连接字符串url中加入ssl=true或者false即可characterEncoding…

    2022年7月16日
    15
  • linux提示8080端口被占用

    linux提示8080端口被占用需要查看 8080 所占用的程序 然后关闭该程序 具体指令如下 lsof i 8080 得到进程号之后 结束该进程 sudokill 9xxxxx 然后再查看 8080 端口发现已经不被占用

    2026年3月18日
    3

发表回复

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

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