【POJ 1011】 Sticks
【题目链接】
http://poj.org/problem?id=1011
【算法】
深搜剪枝
首先我们枚举木棍的长度i,那么就有s/i根木棍,其中s为木棍长度的总和,朴素的做法就是对每种长度进行搜索,然而这样是会超时的,考虑优化 :
优化1 : 优化搜索顺序,将这些木棍的长度从大到小排序,搜索时按递减的顺序选取木棍(优先选择长的木棍)
优化2 : 排除等效亢余,对于每根木棒,记录最近一次尝试的木棒,如果有与最近一次同样长度的木棒,则不尝试,因为这必定也是失败的
优化3 : 排除等效亢余, 如果一根木棒尝试的第一根木棍就失败了,那么直接返回失败,因为其它木棒如果选这根木棍同样是失败的
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 110 int i,s,mx,n,cnt,tmp;
int a[MAXN];
bool used[MAXN]; inline bool dfs(int dep,int now,int last)
{
int i,fail = ;
if (dep > cnt) return true;
if (now == tmp) return dfs(dep+,,);
for (i = last; i <= n; i++)
{
if (!used[i] && now + a[i] <= tmp && fail != a[i])
{
used[i] = true;
if (dfs(dep,now+a[i],i+)) return true;
fail = a[i];
used[i] = false;
if (!now) return false;
}
}
return false;
} int main()
{ while (scanf("%d",&n) && n)
{
s = ; mx = ;
for (i = ; i <= n; i++)
{
scanf("%d",&a[i]);
s += a[i];
mx = max(mx,a[i]);
}
sort(a+,a+n+,greater<int>());
for (i = mx; i <= s; i++)
{
tmp = i;
if (s % i) continue;
cnt = s / i;
memset(used,false,sizeof(used));
if (dfs(,,)) break;
}
printf("%d\n",tmp);
} return ; }
【POJ 1011】 Sticks的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- WEB文件上传之apache common upload使用(一)
文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...
- 常用的SSH注解标签
常用的SSH注解标签 1.Spring的注解 关于配Bean用的 @Component @Controller @Service @Repository 作用 ...
- JS中for循环多个变量的判断原理
看完下面两个例子的比较就明白了,其实就是逗号表达式,总是依据最后一个表达式的值. for(i=0, j=0; i<10, j<6; i++, j++){ k = i + j; consol ...
- Ubuntu用户自定义脚本开机启动
如果想让自己写的脚本随开机自动执行,可以这样做: 编辑文件/etc/init.d/rc.local,在最后添加用户自定义脚本的完整路径即可. 很简单有木有!!
- (转)Hibernate的配置详解
http://blog.csdn.net/yerenyuan_pku/article/details/65041077 在<Hibernate快速入门>一文中,我有讲到Hibernate的 ...
- VBA中Option的四种用法
1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...
- jenkins配置邮件通知
参考: https://www.cnblogs.com/imyalost/p/8781759.html 谢谢大佬~
- cesium的学习
一.学习资料:http://cesiumjs.org/tutorials.html,看完6个教程后对图层加载.控件控制开关.地形数据叠加.模型添加.相机控制.图形绘制有一点了解.这也是cesium的主 ...
- CAD保存高版本的dwg(com接口)
主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgEx 保存DWG文件,可以设置保存的文件版本号和密码,详细说明如下: 参数 说明 IN CString sFile ...
- Spring MVC起步(一)
下图展示了请求使用Spring MVC所经历的所有站点. 在请求离开浏览器时1,会带有用户请求内容的信息,至少会包含请求的URL.但是还可能包含其他的信息,如用户提交的表单. DispatcherSe ...