很经典的搜索题,直接爆搜会卡在连续相同长度的木棍,可以先排序,预处理该长度不行直接跳下一长度木棍的位置

但此题特殊,木棍长度小于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的更多相关文章

  1. poj1011 Sticks (搜索经典好题)

    poj1011 Sticks 题目连接: poj1011 Description George took sticks of the same length and cut them randomly ...

  2. POJ1011 Sticks

    Description George took sticks of the same length and cut them randomly until all parts became at mo ...

  3. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

  4. poj1011 Sticks(DFS+剪枝)

    题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...

  5. poj1011 Sticks[剪枝题]

    https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...

  6. poj-1011 sticks(搜索题)

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  7. poj1011 Sticks (dfs剪枝)

    [题目描述] George took sticks of the same length and cut them randomly until all parts became at most 50 ...

  8. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  9. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  10. 【poj1011】 Sticks

    http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...

随机推荐

  1. Python 文件&异常 初学者笔记

    文件 读取整个文件 with open('pi_30_digits.txt') as file_object :#Python在当前执行文件目录寻找指定文件#filename = 文件的绝对路径或相对 ...

  2. 使用nginx代理gogs遇到推送代码错误的问题(RPC failed; HTTP 413 curl 22 The requested URL returned error: 413)

    前提 代码管理我是用Gogs.Git,前些阵子使用Nginx将git.balabiu.com反向代理到了Gogs的默认端口,其他二级域名准备做其他使用, 导致上报代码出现了错误. 问题 推送代码报错误 ...

  3. cf1266D

    注意到每一个的点出入流是不会变的,因此本质是让构造一张图满足这个出入流并且边上的流量之和最少,显然流量是平衡的,也就是所有节点的出入流之和为0 因此我们可以直接暴力的选择让负数点向正数点连边,连之后就 ...

  4. PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分)

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  5. Qt Gui 第八章

    一.QGradient 该类是渐变画刷相关的类,有三个子类,分别是QConicalGradient.QRadialGradient和QLinearGradient 1.QConicalGradient ...

  6. Sentence by defender

    也许,人在旅途,不是你看清了多少事,而是你看轻了多少事.

  7. ECMAScript基本语法——⑤运算符 比较运算符

    ><>=<======全等于 比较运算符 Java中只能比类型相同的,JavaScript没有限制比较方式 1.类型相同:直接比较 字符串:安装字典顺序比较.按位逐一比较直到比 ...

  8. ContestHunter 1201 最大子序和

    描述 输入一个长度为n的整数序列,从中找出一段不超过m的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7当m=2或m=3时,S=5+1=6 输入 ...

  9. [tensorflow] tf.gather使用方法

    tf.gather:用一个一维的索引数组,将张量中对应索引的向量提取出来 import tensorflow as tf a = tf.Variable([[1,2,3,4,5], [6,7,8,9, ...

  10. 洛谷P1765 手机_NOI导刊2010普及(10) 关于cin和getline的一些区别 以及一些STL

    一. cin>>s:cin>>是由两部分构成的,cin和>>,其中cin是输入流istream类的一个对象,隶属于iostream函数库而>>则是运算符 ...