Java-POJ1011-sticks
很经典的搜索题,直接爆搜会卡在连续相同长度的木棍,可以先排序,预处理该长度不行直接跳下一长度木棍的位置
但此题特殊,木棍长度小于50,我们可以直接桶排序
还有就是关于回溯的理解:
我们写的dfs为的是判断ans是否可行,可行解自然已经被记录下来了,并且一路return即,若回溯到了相同or类似情况,说明必定不能符合题意
TIPS:桶排序+可行性剪枝
package poj.ProblemSet;
import java.util.Scanner;
public class poj1011 {
public static final int MAXN = 100;
public static int[] bucket = new int[MAXN];
public static int ans, flag, sum, maxn, minn;
public static void dfs(int res, int sum, int p) {
if (res == 0||flag==1) { flag = 1;return; }
if (sum == ans) { dfs(res - 1, 0, maxn);return;}
for (int i = p; i >= minn; i--) {
if (bucket[i]>0 && i + sum <= ans) {
bucket[i]--;
dfs(res, sum + i, i);//(*)
bucket[i]++;
if (sum == 0 || sum + i == ans) break;
//执行此句说明(*)行尝试失败,因此到达当前情况的搜索均可剪枝
}
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
for (int n = cin.nextInt(); n != 0; n = cin.nextInt()) {
sum = flag = maxn = 0;minn = MAXN;
for (int i = 0; i < MAXN; i++) bucket[i] = 0;
for(int i=1;i<=n;i++) {
int temp = cin.nextInt();
bucket[temp]++;
sum += temp;
maxn = Math.max(maxn, temp);
minn = Math.min(minn, temp);
}
int INF = sum / 2;
for (int i = maxn; i <= INF; i++)
if (sum % i == 0) {
ans = i;
dfs(sum / i, 0, maxn);
if(flag==1)break;
}
if (flag == 1) System.out.println(ans);
else System.out.println(sum);
}
}
}
Java-POJ1011-sticks的更多相关文章
- poj1011 Sticks (搜索经典好题)
poj1011 Sticks 题目连接: poj1011 Description George took sticks of the same length and cut them randomly ...
- POJ1011 Sticks
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- poj1011 Sticks(DFS+剪枝)
题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...
- poj1011 Sticks[剪枝题]
https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...
- poj-1011 sticks(搜索题)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- poj1011 Sticks (dfs剪枝)
[题目描述] George took sticks of the same length and cut them randomly until all parts became at most 50 ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 【poj1011】 Sticks
http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...
随机推荐
- C#加密与解密(DES\RSA)学习笔记
本笔记摘抄自:https://www.cnblogs.com/skylaugh/archive/2011/07/12/2103572.html,记录一下学习过程以备后续查用. 数据加密技术是网络中最基 ...
- Python第三方库requests的编码问题
PS:这个解决方法可能很简单,但是这是平时的一些细节问题,所以有必要提醒一下! 首先代码不多,就是通过get方法去获取豆瓣首页信息,如图:但是会报UnicodeEncodeError: 'gbk' c ...
- PAT (Basic Level) Practice (中文)1041 考试座位号 (15 分)
每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考 ...
- 在linux中安装nginx
linux系统安装在vmware中,首先在主机中利用shell工具与虚拟机连接 1.在linux中查看虚拟机的ip地址 在终端输入 ifconfig 红框里面就是ip地址 2.在主机中打开shell工 ...
- 844. 走迷宫(bfs模板)
给定一个n*m的二维整数数组,用来表示一个迷宫,数组中只包含0或1,其中0表示可以走的路,1表示不可通过的墙壁. 最初,有一个人位于左上角(1, 1)处,已知该人每次可以向上.下.左.右任意一个方向移 ...
- yii2 生成随机字符串
uuid uuid use Faker\Provider\Uuid; Uuid::uuid(); yii自带 生成32位字符串 Yii::$app->getSecurity()->gene ...
- Ubuntu 打不开终端 侧边栏消失的解决办法
在网上找了很多办法,大多不行,具体原因也不太清楚,应该是Unity某些配置被改了. 我是在ubuntu14.04平台利用apt-get卸载python后,关机重启出现"打不开终端和侧边栏消失 ...
- 51Nod 1449 砝码称重 (二进制思想)
现在有好多种砝码,他们的重量是 w0,w1,w2,... 每种各一个.问用这些砝码能不能表示一个重量为m的东西. 样例解释:可以将重物和3放到一个托盘中,9和1放到另外一个托盘中. Input 单组 ...
- Selenium3+python自动化007-警告框
警告框 alert = driver.switch_to.alert alert.text() alert.accpet() alert.dismiss() # 导selenium包 from sel ...
- Linux常用命令: zip、unzip 压缩和解压缩命令
zip基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] 常用参数: -a 将文件转成ASCII模式 -F 尝试修复损坏的压缩文件 -h 显示帮助界面 -m 将文件压缩之后,删除源 ...