【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. ...
随机推荐
- Miller Rabin 大素数测试
PS:本人第一次写随笔,写的不好请见谅. 接触MillerRabin算法大概是一年前,看到这个算法首先得为它的神奇之处大为赞叹,竟然可以通过几次随机数据的猜测就能判断出这数是否是素数,虽然说是有误差率 ...
- CUDA-GPU编程
参考:http://blog.csdn.net/augusdi/article/details/12833235 第二节 新建NVIDIA项目: 新建项目及会生成一个简单的代码demo,计算矩阵的加 ...
- html5——动画案例(大海)
太阳的发散效果主要是利用transform: scale(1.3),将物体变大 <!DOCTYPE html> <html lang="en"> <h ...
- VC++ 遍历文件夹
}; strcpy_s(szFind, MAX_PATH, m_szDir); strcat_s(szFind, "\\*.*"); WIN32_FIND_DATA wfd; HA ...
- 12、scala函数式编程集合
1.Scala的集合体系结构 2.List 3.LikedList 4.Set 5.集合的函数式编程 6.函数式编程综合案例:统计单词总数 1.Scala的集合体系结构 Scala中集合体系主要包括: ...
- Linux下清空文件的常用方法
1. 本人用的最多,感觉也是最方便的: > filename.log 2. : > filename.log 3. cat /dev/null > filename.log
- CAD动态绘制带面积周长的圆(com接口)
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- php第十二节课
练习 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- codevs1231 最优布线问题
1231 最优布线问题 题目描述 Description 学校需要将n台计算机连接起来,不同的2台计算机之间的连接费用可能是不同的.为了节省费用,我们考虑采用间接数据传输结束,就是一台计算机可以间接地 ...
- 理解Mysql prepare预处理语句
MySQL 5.1对服务器一方的预制语句提供支持.如果您使用合适的客户端编程界面,则这种支持可以发挥在MySQL 4.1中实施的高效客户端/服务器二进制协议的优势.候选界面包括MySQL C API客 ...