【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. ...
随机推荐
- MySQL实现当前数据表的所有时间都增加或减少指定的时间间隔
DATE_ADD() 函数向日期添加指定的时间间隔. 当前表所有数据都往后增加一天时间: UPDATE ACT_BlockNum SET CreateTime = DATE_ADD(CreateTim ...
- css图片特效
网站图片往往有很多显示效果,使用css是实现图片特效的比较简便的方式.下面记录一段css鼠标指向的多重特效: <!DOCTYPE html><html lang="en&q ...
- JS——arguments
1.只在函数中使用 2.返回的是实参的数组 <script> getNum(1, 2);//(2) [1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ ...
- JQuery文档加载完成执行js的几种方法
js中文档加载完毕.一般在body加一个onload事件或者window.onload = function () {} jQuery中有好多写法,平时也不注意,别人一问,还真觉得头大. 下面是我整理 ...
- TCP报文到达确认(ACK)机制
TCP数据包中的序列号(Sequence Number)不是以报文段来进行编号的,而是将连接生存周期内传输的所有数据当作一个字节流,序列号就是整个字节流中每个字节的编号.一个TCP数据包中包含多个字节 ...
- strcmp 与 _tcscmp
strcmp 用来比较ANSI字符串,而_tcscmp用来比较UNICODE(宽字符)的字符串.ANSI字符串中,1个英文字母为1个字节,1个中文字符为2个字节,遇到0字符表示字符串结束.而在UNIC ...
- CAD插入jpg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 常见的Xshell运行命令
最近接触到了Xshell这个软件,使用这个软件我们来进行连接Linux系统,进去之后我们可能会两眼一抹黑,小编就带大家来学些常见的shell命令. 首先我们要跟大家从最简单的聊起,我们进入Xshell ...
- LNOI2019 退役记
Day -4 最近这两天智商有点不在线啊..得好好调整作息了,滚粗感++ 复习模板好啊 下午睡了一觉,智商似乎回来了一丢丢,滚粗感-- Day -3 智商略有回暖,滚粗感-- 明天有模拟赛,要加油啊 ...
- 模拟Spring容器的getBean方法(Maven工程)
Spring容器的getBean方法是通过反射机制实现的,下面的测试程序模拟getBean的实现原理. 步骤一:pom.xml文件配置解析XML文件的dom4j.jar 步骤二:XML文件中配置bea ...