搜索 + 剪枝 --- POJ 1101 : Sticks
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的更多相关文章
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- 搜索+剪枝 POJ 1416 Shredding Company
POJ 1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5231 Accep ...
- POJ 1011 - Sticks DFS+剪枝
POJ 1011 - Sticks 题意: 一把等长的木段被随机砍成 n 条小木条 已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析: 1. 该长度必能被总长整除 ...
- 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move
poj 1568:Find the Winning Move [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...
- NOIP2015 斗地主(搜索+剪枝)
4325: NOIP2015 斗地主 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 270 Solved: 192[Submit][Status] ...
- hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)
题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...
- hdu 5887 搜索+剪枝
Herbs Gathering Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
随机推荐
- Three Sources of a Solid Object-Oriented Design
pingback :http://java.sys-con.com/node/84633?page=0,1 Object-oriented design is like an alloy consis ...
- [Scheme]Understanding the Yin-Yang Puzzle
这题目确实比较杀脑细胞... 原题: (let* ((yin ((lambda (cc) (display "@") cc) (call-with-current-continua ...
- 在WPF中使用字体图标
一.源码描述 这是一款基于WPF窗体应用程序的字体图标示例源码, 该源码简单易懂使用于初学者和实战项目应用, 感兴趣的朋友们可以下载看看哦. 二.功能介绍 1.用ICO字体代替 ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- EntityFramework Code First 添加唯一键
在EntityFramework 6.1后可以直接使用 [Index("TitleIndex", IsUnique = true)] public string Title { g ...
- sql server trace 和 Profiler
MS SQL Server Profiler概述: MS SQL Server Profiler是SQL Trace的GUI接口,提供对SQL Server Database Engine ...
- 调用 google speech api (使用Google语音识别引擎)
完全参考自: http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/ http://aiku.me/bar/104480 ...
- [Z] 计算机类会议期刊根据引用数排名
一位cornell的教授做的计算机类期刊会议依据Microsoft Research引用数的排名 link:http://www.cs.cornell.edu/andru/csconf.html Th ...
- 关于IT概念的一些思考
同事提及“软件工程.软件生命周期.项目管理.CMMI.IPD.RUP.UML及UML建模.面向对象分析与设计.需求分析.系统分析与设计……等等,它们到底是什么?它们之间有什么关系?” 下面是个人见 ...
- mssql表名列名对应语句
if exists (select * from tempdb..sysobjects where name like '#magic%') drop table #magic go select a ...