【Link】:

【Description】



给你最多n个棍子;

(n< = 64)

每根棍子长度(1..50)

问你这n根棍子,可以是由多少根长度为x的棍子分割出来的;

x要求最小

【Solution】



首先,将各根棍子的长度求和->sum

最后的长度x肯定是sum的因子;

则枚举x从各根棍子长度的最大值sum作为因子;

枚举量假设为len;

然后一直用剩余的棍子去凑这个长度len

凑够了,就重新选择剩下的棍子,继续凑len;

剪枝:

1.还需要凑的量为len,但是尝试用完某根棍子之后,无法凑够,则直接return,因为表示有一根棍子不能够被加入到最后的答案中

2.剩余的棍子长度为sum,然后发现第一根尝试的棍子不能凑够,同理也退出

3.一根棍子用了之后,发现不行,则不再用这根棍子凑了,把相同的棍子跳过

4.从大到小排序,这样剪枝的效果会快一些出现



【NumberOf WA】



1



【Reviw】



凑够一根,然后继续凑;

(再从1开始);

记录之前用过哪一根;

没凑够的话,就从下一根根子开始;



【Code】

#include <cstdio>
#include <algorithm>
using namespace std; const int N = 64; int l[N+20],n,sum,now,vis[N+20]; bool dfs(int pre,int need,int rest){
if (rest==0) return true; for (int i = pre;i <= n;i++)
if (!vis[i] && l[i]<=need){
vis[i] = 1;
if (l[i]==need && dfs(1,now,rest-l[i]))
return true;
else if ( dfs(i+1,need-l[i],rest-l[i]))
return true;
vis[i] = 0;
if (rest==sum) return false;
if (need==now) return false;
while (i+1<=n && l[i+1]==l[i]) i++;
} return false;
} int main(){
//freopen("F:\\rush.txt","r",stdin);
while (~scanf("%d",&n)){
if (n==0) break;
sum = 0;
for (int i = 1;i <= n;i++)
scanf("%d",&l[i]),sum+=l[i];
for (int i = 1;i <= n;i++) vis[i] = 0;
sort(l+1,l+1+n);
reverse(l+1,l+1+n);
int ans = sum;
for (now = l[1];now < sum;now++)
if (sum%now==0){
if (dfs(1,now,sum)){
ans = now;
//printf("%d\n",now);
break;
}
}
printf("%d\n",ans);
}
return 0;
}

【Uva 307】Sticks的更多相关文章

  1. 【Uva 10003】Cutting Sticks

    [Link]: [Description] 给你一根长度为l的棍子; 然后有n个切割点; 要求在每个切割点都要切割一下; 这样,最后就能形成n+1根小棍子了; 问你怎样切割,消耗的体力最小; 认为,消 ...

  2. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  3. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  4. 【UVa 10881】Piotr's Ants

    Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...

  5. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  6. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. 【uva 1025】A Spy in the Metro

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. 【POJ 1011】 Sticks

    [题目链接] http://poj.org/problem?id=1011 [算法] 深搜剪枝 首先我们枚举木棍的长度i,那么就有s/i根木棍,其中s为木棍长度的总和,朴素的做法就是对每种长度进行搜索 ...

随机推荐

  1. POJ 1743 Musical Theme 后缀数组 不可重叠最长反复子串

    二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include & ...

  2. Linux下安装Mysql(RPM安装)

    一.去官网下载本次安装须要的mysql版本号.我们须要安装的是5.1版本号的且centos系统是64位的.所下面载的是MySQL-community-5.1.73-1.rhel5.x86_64.rpm ...

  3. node03--http

    form.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. angularjs $location 服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. Autofac依赖注入框架

    最近使用Autofac框架做项目的依赖注入,感觉挺好用的. 没有深入研究,只是拿来用用,具体可以去官网看看:https://autofac.org/. 这里只是贴一下最近项目的配置: public p ...

  6. Lamp安装 php-v5.6【ZendGuardLoader】的问题

    Lamp安装 php-v5.6[ZendGuardLoader]的问题 标签(空格分隔):php,linux Apache日志: 就这个问题导致无法解析运行php文件.下面是网上找的解决方案 Zend ...

  7. CUDA笔记(十)

    下午仔细研究了两个程序,然后搜了一下解决方法 http://blog.sina.com.cn/s/blog_6de28fbd01011cru.html http://blog.csdn.net/che ...

  8. 虚拟机下安装mysql

    虚拟机下CentOS6.8下安装MYSQL5.6 方法: 整理修改于 http://www.cnblogs.com/liuyi2614/p/6382183.html 开始时: 普通用户是$ root用 ...

  9. jqueryEasyui重新渲染

    转载: jQuery EasyUI parser 的使用场景 | WebUI框架使用参考+http://www.easyui.info/archives/216.html parser,故名意思,就是 ...

  10. HDU 5214 Movie【贪心】

    题意:给出n,l[1],r[1],a,b,c,d 其中 l[i]=l[i-1]*a+b,r[i]=r[i-1]*c+d; 问能否从这n个区间中选出不相交不重叠的三个区间 看的题解 先考虑最左边的区间, ...