【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. ...
随机推荐
- html5——语义标签
传统布局 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- HDU_2212_水
DFS Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 微信小程序音频长度获取的问题
小程序推荐使用wx.createInnerAudioContext()创建的innerAudioContext,我们也通过这个接口创建音频.音频的长度可以通过属性获取: 但是,给innerAudioC ...
- 配置Android的NDK开发环境(eclipse)
ndk下载地址: http://blog.csdn.net/zhanghuoding/article/details/51345256 在eclipse设置ndk位置 右键你的工程,android t ...
- NOIP2018 滚粗记
Day -2 上午,大家都在复习各种模板,zhx总结了足足67个模板(杨辉三角也算模板???),lgl死磕FFT发现cos和sin打反了,我也是复习板子和以前做过的题,几乎没有人颓. 接着jdr,l ...
- 20.混合使用match和近似匹配实现召回率与精准度的平衡
主要知识点: 召回率的慨念 精准度的慨念 match和近似匹配混合使用方法 召回率(recall):比如你搜索一个java spark,总共有100个doc,能返回多少个doc作为结果 ...
- 11.best fields策略(dis_max参数设置)
主要知识点 常规multi-field搜索结果分析 dis_max参数设置 一.为帖子数据增加content字段 POST /forum/article/_bulk { "u ...
- Leetcode 41.缺失的第一个正数
缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: ...
- iOS学习笔记18-CoreData你懂的
一.CoreData介绍 CoreData是iOS5之后新出来的的一个框架, 是对SQLite进行一层封装升级后的一种数据持久化方式. 它提供了对象<-->关系映射的功能,即能够将OC对象 ...
- CodeForces - 284C - Cows and Sequence
先上题目: C. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input s ...