POJ1011 Sticks
POJ1011 Sticks
剪枝好题,具体见代码:
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <cctype> 5 #include <iostream> 6 using namespace std; 7 8 inline int read() 9 { 10 int f=1,x=0; char ch; 11 while(!isdigit(ch=getchar())) if(ch=='-') f=-1; 12 while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); 13 return f*x; 14 } 15 16 #define res register int 17 const int N=100; 18 int a[N],used[N],n,sum,m,ok,len; 19 bool cmp(const int &x,const int &y){return x>y;} 20 21 void dfs(int now,int last,int rest) 22 { 23 if(rest==0) 24 { 25 int i; 26 if(now==m) {ok=1; return ;} 27 for(i=1 ; i<=n ; i++) if(!used[i]) break; 28 used[i]=1; dfs(now+1,i,len-a[i]); 29 used[i]=0; if(ok) return ; 30 } 31 32 for(res i=last+1 ; i<=n ; i++) 33 { 34 if(!used[i]&&a[i]<=rest) 35 { 36 used[i]=1; dfs(now,i,rest-a[i]); used[i]=0; 37 if(ok) return ; 38 while(a[i+1]==a[i]) i++;// 39 if(rest==len||rest==a[i]) return ; 40 } 41 if(i==n) return ; 42 } 43 } 44 45 /*剪枝: 46 1.把小木棍从大到小排序,先拼大的 47 2.从小到大枚举原始木块长度len,成立了就直接输出答案。范围:最短的木棍到所有木棍 48 长度之和的一半,且len需要整除sum 49 3.当一个木棍不能拼时,和它长度相等的木棍显然也不能拼 50 4.枚举下一个要拼的木棍时只需从上一个用过的木块的下一个开始枚举 51 ****5.如果当前长棍剩余的未拼长度等于当前正在试着拼的木块的长度或者等于原始长度(即 52 刚开始拼),继续拼下去却失败了,则直接回溯,结束枚举。 53 */ 54 int main() 55 { 56 57 while(scanf("%d",&n)==1 && n) 58 { 59 sum=0;ok=0; 60 memset(used,0,sizeof(used)); 61 memset(a,0,sizeof(a)); 62 for(res i=1 ; i<=n ; i++) 63 { 64 a[i]=read(); 65 sum+=a[i]; 66 } 67 sort(a+1,a+n+1,cmp); 68 for(len=a[1] ; len<=sum/2+1 ; len++) 69 { 70 if(sum%len!=0) continue; 71 used[1]=1; m=sum/len; 72 ok=0; 73 dfs(1,1,len-a[1]); 74 used[1]=0; 75 if(ok) 76 { 77 printf("%d\n",len); 78 break; 79 } 80 } 81 if(!ok) printf("%d\n",sum); 82 } 83 return 0; 84 }
View Code
转载于:https://www.cnblogs.com/wmq12138/p/10369150.html
POJ1011 Sticks相关推荐
- POJ1011———Sticks
/*dfs,剪枝是关键.╮(╯▽╰)╭本来是想一根棍子 一个棍子的填充,遇到棍子不合适的就回溯.但是实验 证明这绝对是剪错方向的!正确的解法是...先寻找 第一根棍子合不合适,如果不合适就没有必要找下 ...
- ACM题集以及各种总结大全(转)
ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...
- 竞赛程序设计知识要点图谱
一.基础算法 1.枚举法 POJ1248 Safecracker HDU1172 猜数字 POJ1543 Perfect Cubes POJ1046 Color Me Less 2.递归法:Hanoi ...
- POJ1011 HDU1455 UVA307 UVALive5522 Sticks题解
代码来源:DeathYmz AC的C++语言程序如下: #include<cstdio> #include<iostream> #include<cstring> ...
- HDU1051Wooden Sticks
Wooden Sticks http://acm.hdu.edu.cn/showproblem.php?pid=1051 #include<stdio.h> struct stick{ i ...
- UVa307 - Sticks(DFS+剪枝)
题意:给出若干个一样长的sticks,剪断后有n个部分,问原来sticks的最小长度. 思路:首先要确定sticks的长度范围,范围应该在[max(n1,n2..., n), sum(n1,n2... ...
- HDU-1051 Wooden Sticks
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 欧拉路径 之 poj 2513 Colored Sticks
/* 欧拉路径 之 poj 2513 Colored Sticks欧拉路径: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径.无向图存在欧拉路径 充要条件:1) 图是连通的 ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MB Submit: xxx ...
最新文章
- 机器翻译注意力机制及其PyTorch实现
- linux系统中条码如何识别的,如何(可靠地)在嵌入式(无头)Linux中读取USB条形码扫描仪?...
- python 参数解析器_Python参数解析器,在h之前引发异常
- Docker容器间Link单向通信
- wpa_supplicant与wpa_cli之间通信过程
- windows 7搭建流媒体服务
- linux 带ifdef运行程序_如何让linux的一段程序代码进入内核态运行
- Illustrator 教程,如何在 Illustrator 中裁剪、分割和修剪作品?
- 第一讲(3)osgearth编译
- Maven实战 PDF 许晓斌
- 2020.10.12--PS--制作图章、抽出滤镜、消失点
- GIT文档同步MinDoc - MinDoc模拟登陆
- 不同时区时间换算_世界时区划分时差在线查询计算_时间换算器
- 漫漫人生录 | 一个月薪 120000 的[ 北京 ] 程序员的真实生活
- R 中 facet_wrap() 和 facet_grid() 的区别
- 算法练习-Apples Prologue
- 高职单招面试流程及技巧有哪些
- 用DownSub 导出TED和youtube字幕
- 2022年总结:打开新世界,踏上新征程
- first diary
热门文章
- Kubernetes 常见问题总结
- zabbix 通过 zabbix-agent 方式监控 Windows主机
- ES组件elasticsearch-head报错 npm ERR! Please include the following file with any support request
- 【HDFS】HDFS操作命令
- 网络管理与维护作业11
- failed to open stream :HTTP request failed 解决方法
- visual studio 容器工具首次加载太慢 vsdbg\vs2017u5 exists, deleting 的解决方案
- identifier is too long 异常处理
- VS2010无法调试问题解决
- ViewPager PagerAdapter未更新视图