poj sticks 木棍 枚举+搜索+小技巧
2015-12-03 18:29
267 查看
这大概是我复赛前看过的题了,那个时候的我其实还不是很会打搜索,甚至认为搜索,枚举的效率都是很低的,是不能够把题AC的,后来为了改变这种看法(其实是因为我发现一些我想不出的题的AC程序里大多有搜索,枚举的算法,格外揪心,没想到竟这么简单)。才做了一些搜索的题。这就是那些年曾令我揪心的题之一,想不出,去百度,这么简单,心碎了。的确搜索如果剪枝做的好的话,是可以把一些题目AC的。
、 好吧!可打完后提交的第一次还是TLE了,心好累。去别人的博客园看了一下,其实大家都差不多。仔细一看才发现别人是降序的,而我是升序的。这估计就是这题的一点小技巧吧!弄完后,AC了,既然经验在此,就积累了吧,毕竟积少成多。
、 好吧!可打完后提交的第一次还是TLE了,心好累。去别人的博客园看了一下,其实大家都差不多。仔细一看才发现别人是降序的,而我是升序的。这估计就是这题的一点小技巧吧!弄完后,AC了,既然经验在此,就积累了吧,毕竟积少成多。
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<set> #define maxn 70 #define rep(i,j,k) for(int i = j; i <= k; i++) #define st stick[i] using namespace std; int stick[maxn] = {0}, n, used[maxn] = {0}, ch, mu; int unable[maxn] = {0}; bool bmp(int a,int b) { return a > b; } inline int read() { int s = 0, t = 1; char c = getchar(); while( !isdigit(c) ){ if( c == '-' ) t = -1; c = getchar(); } while( isdigit(c) ){ s = s * 10 + c - '0'; c = getchar(); } return s * t; } bool match(int sheng,int x,int ci) { if( !sheng ) ci++, x = 0, sheng = ch; if( ci == mu ) return 1; rep(i,x,n-1){ if( st <= sheng && !used[i] ){ used[i] = 1; if( match(sheng-st,i+1,ci) ) return 1; else if( x == 0 ) { used[i] = 0; return 0; } used[i] = 0; int j = i; while( stick[j+1] == stick[i] ) j++; i = j; } } return 0; } int main() { n; while( scanf("%d", &n) == 1 && n ){ int sum = 0; rep(i,0,n-1){ st = read(); sum += st; used[i] = 0; } sort(stick,stick+n,bmp); int maxl = stick[0]; rep(i,maxl,sum){ if( sum % i == 0 ){ ch = i; mu = sum / i; if( match(i,0,0) ){ cout<<i<<endl; break; } } } } return 0; }
相关文章推荐
- CentOS系统在不重启的情况下为虚拟机添加新硬盘
- php网站多语言
- 个性图片预览和多点触控(伪代码)
- Android中处理崩溃异常
- 前端--关于CSS文本
- Sql Server xml 类型字段的增删改查
- iOS UITabBarController tabbar 上面的文字title 怎么移动位置
- 使用Java编写并运行Spark应用程序
- Valid Anagram
- 汇编学习(1)
- 【蓝桥第五周】杨辉三角形
- miniui中的相关问题
- php缓存技术总结
- Junit4的使用方法
- iOS 点击子视图不让其响应父视图手势
- hdu 1403 Longest Common Substring 后缀数组da算法
- 移动端 触屏滑动条菜单(完善版 转)
- The beginning iOS8 Programming with Swift 中文翻译 - 2
- 变量的声明以及基本的运算符
- [python]类