Sticks

Problem 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 file 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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 100;
int n;
int s[N];
int v[N];
int t, length; //个数 长度
bool cmp(int a, int b){
return a > b;
}
bool dfs(int k, int len, int pos){ //第k根 第k跟长度 下标 //程序出口
if(k == t) return true; // 进来的时候肯定是 k = t, len = 0, pos = ? 最后一根的长度肯定是刚好等于 length !! if(len == length) return dfs(k+1, 0, 0); //拼接下一根 for(int i = pos+1; i <= n; i++){
if(!v[i] && len+s[i] <= length){
v[i] = 1;
if(dfs(k, len+s[i], i)) return true; //这种情况可以 下面的就不用看了
v[i] = 0; // len+s[i] 这种情况不行
if(len == 0) return false; //拼接第 k 跟木棒的 第一节的时候 不能拼成 length return false
while(s[i] == s[i+1]){ //相同的就不用看了
i++;
}
}
}
return false;
}
int main(){
while(~scanf("%d", &n) && n){
int sum = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &s[i]);
sum += s[i];
}
sort(s+1, s+1+n, cmp); //降序
for(int i = s[1]; i <= sum; i++){
if(sum%i == 0){
memset(v, 0, sizeof v);
length = i;
t = sum / i; //组成的木棒个数
if(dfs(1, 0, 0)){//当前木棒数, 当前木棒数的长度, 下标
printf("%d\n", i);
break;
}
}
}
}
return 0;
}

hdu 1145(Sticks) DFS剪枝的更多相关文章

  1. poj 1011 :Sticks (dfs+剪枝)

    题意:给出n根小棒的长度stick[i],已知这n根小棒原本由若干根长度相同的长木棒(原棒)分解而来.求出原棒的最小可能长度. 思路:dfs+剪枝.蛮经典的题目,重点在于dfs剪枝的设计.先说先具体的 ...

  2. poj1011 Sticks(dfs+剪枝)

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

  3. HDU 1455 Sticks(经典剪枝)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  5. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  6. HDU 1175 连连看 (DFS+剪枝)

    <题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...

  7. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  9. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

随机推荐

  1. LT4020替代方案

    国产  替代LT4020的方案 南芯 展讯的方案 https://item.taobao.com/item.htm?spm=a230r.1.14.21.6f27bf96rrAtci&id=56 ...

  2. Jlink固件更新

    故障:JLINK上的指示灯也不亮,无驱动等 文中所提工具和固件链接: 工具:http://pan.baidu.com/s/1c2z8nao 固件:http://pan.baidu.com/s/1jIB ...

  3. gpgj-12 ROIC估值法总结

    1.公司甲乙丙 ·公司的分类: 1.ROIC  <    10%的公司        不赚钱的公司,价值摧毁者,避免投资这种 2.ROIC 约等于 10%的公司        勉强能够糊口,EV ...

  4. DS1302应用电路

    http://www.diangon.com/wenku/rd/danpianji/201501/00017904.html

  5. easyui datagrid 自定义单元格单击与双击事件(Day_38)

    $(function(){ $('#tableId').datagrid({//单击事件   onClickRow:function(rowIndex,rowData){  alert("单 ...

  6. python3操作Kafka

    # -- coding: UTF-8 import datetime import json import time from kafka import KafkaProducer producer= ...

  7. Java读取SQL server数据库

    要打开SQL server 的三个服务,然后再执行代码. package com.sql; import java.sql.SQLException; import java.sql.Statemen ...

  8. cmodel模拟器开发

    cmodel模拟器开发 对于一个公司来说,产品的设计周期就是生命线,一般来说都会在设计功能级仿真的c-model后直接转向RTL设计. 在目前的技术下,做cycle-by-cycle的设计和直接RTL ...

  9. CVPR2020论文解析:实例分割算法

    CVPR2020论文解析:实例分割算法 BlendMask: Top-Down Meets Bottom-Up for Instance Segmentation 论文链接:https://arxiv ...

  10. 基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化

    基于自动驾驶车辆的NVIDIA-TensorRT推理实时优化 Optimizing NVIDIA TensorRT Conversion for Real-time Inference on Auto ...