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

但此题特殊,木棍长度小于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. 关于 setw() 函数(C++)

    // about setw() #include <iostream> #include <iomanip> #include <cstring> using na ...

  2. 【剑指Offer】01、二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  3. mysql 数据库中存在重复记录,删除保留其中一条

    DELETE FROM people WHERE peopleName IN ( SELECT peopleName FROM people GROUP BY peopleName HAVING ) ...

  4. 关于SkinTextBox中SkinTxt的事件使用

    SkinTxt的事件中的事件不能像一般的事件那样在属性中直接双击生成,它里面的事件都需要进行一定的转换间接使用,具体用法如下: 1.手动编写事件需要做的操作的方法 public void txtCod ...

  5. reids中删除某个前缀的所有key

    需求:reids中删除某个前缀的所有key 说明:代码中的0:2标识从key前缀中截取前2个字符,这里示例的时候比如“b_”前缀,使用时候根据实际情况截取对应的长度进行判断即可. 生成测试数据 #!/ ...

  6. Udacity_deep_learning_anconda

    1.创建anconda的虚拟环境: conda create -n your_env_name Python=X.X(2.7.3.6等) 2.查看anconda 有哪些虚拟环境: conda env ...

  7. ubantu中的apache中设置代理

    1.启用代理模块 a2enmod proxy proxy_balancer proxy_http 2.修改 /sites-available/000-default.conf 添加 <Virtu ...

  8. lampp ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'lepus'

    解决方法: 在[mysqlld]段下增加如下代码:skip-grant-tables: 1.which mysql 查看mysql位置,例如:/opt/lampp/bin/mysql 2.进入配置my ...

  9. Xmanager6

    Xmanager6企业版 6.0096 含产品秘钥: https://www.newasp.net/soft/467373.html

  10. npm 升级到最新版本

    先npm -v查看自己的npm 是否是最新版本,如果不是则进入安装node的文件夹,可通过 where node 查找该文件夹. 进入之后使用: npm i npm -g 之后使用: npm -v 查 ...