Sticks

Problem's Link:   http://poj.org/problem?id=1011


Mean:

http://poj.org/problem?id=1011&lang=zh-CN&change=true

analyse:

爆搜,但是其中蕴含着很多剪枝。

Time complexity: O(n^2)

Source code: 

//  Memory   Time
// 1347K 0MS
// by : Snarl_jsb
// 2014-11-07-17.14
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std; int n,sum,len;
vector<int> sti(65);
vector<bool> used(sti.size()); // k-----从第k根开始往后判断
// total-----该回合还剩下的长度
// sum----未选取的棍子的总长度
bool dfs(int k,int total,int sum)
{
if(total==0)
{
sum-=len;
if(sum==0)
{
return true;
}
else
{
total=len;
for(k=0;used[k];++k); //找出未选的最靠前的一根
used[k]=1;
//由于从第k根开始选,第k根必选,从k+1根开始搜索
if(dfs(k+1,total-sti[k],sum))
return true;
used[k]=0;
sum+=len;
}
}
else
{
for(int i=k;i<n;++i)
{
if(sti[i]==sti[i-1]&&(!used[i-1])&&i>0)
continue;
if(total>=sti[i]&&(!used[i]))
{
total-=sti[i];
used[i]=1;
if(dfs(i,total,sum))
return true;
total+=sti[i];
used[i]=0;
if(sti[i]==total)  //剪枝:该段的长度正好等于需要的长度,但是该方案行不通,直接返回false退出该函数
   break;
}
}
}
return false;
} bool cmp(int a,int b)
{
return a>b;
} int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
while(cin>>n,n)
{
sum=0;
int tmp;
sti.clear();
for(int i=0;i<n;++i)
{
used[i]=0;
cin>>tmp;
sum+=tmp;
sti.push_back(tmp);
}
sort(sti.begin(),sti.end(),cmp);
bool flag=false;
for(len=sti.front();len<=sum/2;++len)
{
if(sum%len==0)
{
if(dfs(0,len,sum))
{
cout<<len<<endl;
flag=1;
break;
}
}
}
if(!flag)
cout<<sum<<endl;
}
return 0;
}
/*
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
8
45 56 78 456 1231 456456 45 123
*/

  

搜索 + 剪枝 --- POJ 1101 : Sticks的更多相关文章

  1. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  2. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  3. 搜索+剪枝 POJ 1416 Shredding Company

    POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accep ...

  4. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  5. 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move

    poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...

  6. NOIP2015 斗地主(搜索+剪枝)

    4325: NOIP2015 斗地主 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 270  Solved: 192[Submit][Status] ...

  7. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  8. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. hdu 5113(2014北京—搜索+剪枝)

    题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...

随机推荐

  1. 将Sql Server迁移到Always on集群 - 账号的同步

    Always on环境的建立,网上资料很多,主要是windows集群的建立以及Sql Server Always on的建立,略 容易忽略的是Sql server账号同步问题(Always on能实现 ...

  2. Visual Studio 2008 使用 WinCE 5.0 Emulator

    1. 簡介 由於 Visua Studio 2008 與 WinCE 5.0 Emulator 並沒有完全整合,因此想要測試我們所開發的 Windows CE 程式,需透過設定,將我們所開發的程式丟到 ...

  3. nginx模块开发篇 (阿里著作)

    背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...

  4. .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework

    错误描述: The 'targetFramework' attribute in the <compilation> element of the Web.config file is u ...

  5. [转] Visual Studio Code behind a proxy

    http://www.tuicool.com/articles/jyyIBf3 http://blog.majcica.com/2016/04/07/visual-studio-code-behind ...

  6. AppStore 相关

    App 跳转 AppStore 网址链接   https://itunes.apple.com/app/uri/id582319843?mt=8   https 可替换成 itms,可直接避免进入 S ...

  7. Gamma校正与线性工作流

    1 Gamma校正是什么?8位亮度值x(0-1)经过x^0.45的一个提亮过程. 2 为什么需要Gamma校正 人的眼睛是以非线性方式感知亮度,在自然界中,人感觉到的一半亮度其实只有全部能量的0.2, ...

  8. 【转】JavaScript中的对象复制(Object Clone)

    JavaScript中并没有直接提供对象复制(Object Clone)的方法.因此下面的代码中改变对象b的时候,也就改变了对象a. a = {k1:1, k2:2, k3:3}; b = a; b. ...

  9. Fabric自动部署太方便了

    之前不知道有Fabric工具,每次发布程序到服务器上的时候,基本流程:本地打包程序 -> Ftp上传 -> 停服务器Apache -> 覆盖文件 -> 启动Apache, 非常 ...

  10. sizeof()用法汇总

    sizeof()功能:计算数据空间的字节数 1.与strlen()比较      strlen()计算字符数组的字符数,以"\0"为结束判断,不计算为'\0'的数组元素.      ...