POJ1011 Sticks
Description
Input
Output
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
Source
中文:
Description
Input
Output
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
Source
Translator
正解:搜索+各种强力剪枝
解题报告:
明天就要去北京参加夏令营了,今晚练一练手感。
一直知道这道经典搜索题,然而一直没有打。今天打了一下,高兴地一遍过样例,然后交一发高兴地TLE。于是自己yy出了几个剪枝,调了一下,感觉快了很多,然而交了之后还是TLE,
无奈上网蒯剪枝,发现如果当前需要搜一根新棒并且当前最长的那根用进去不可以那么可以直接return,这个强力剪枝让我直接AC了。
调了1个多小时。。。
其他的剪枝还是比较好想到的吧。
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int n;
int a[MAXN];
int ans,total;
int ljh;
bool ok;
bool use[MAXN]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool cmp(int q,int qq){ return q>qq; } inline void Init(){
total=;
for(int i=;i<=n;i++) a[i]=getint(),total+=a[i];
sort(a+,a+n+,cmp);
ans=a[]; ok=false;
memset(use,,sizeof(use));
} inline void dfs(int now,int len,int zong){
if(zong==n) { ok=; return ; }
int cun=-;//上一个不可行的值
for(int i=now;i<=n;i++) {
if(use[i] || cun==a[i]) continue;
use[i]=;
if(a[i]+len<ans){
dfs(i,a[i]+len,zong+);
if(ok) return ;
else cun=a[i];
}
else if(a[i]+len==ans){
dfs(,,zong+);//从0开始
if(ok) return ;
else cun=a[i];
}
use[i]=;
if(len==) break;//构建新棒时,对于新棒的第一根棒子,在搜索完所有棒子后都无法组合,则说明该棒子无法在当前组合方式下组合,不用往下搜索(往下搜索会令该棒子被舍弃),直接返回上一层
}
} inline void solve(){
while(scanf("%d",&n)!=EOF) {
if(n==) break;
Init();
for(;ans<=total-ans;ans++) //只需判断到一半
if(total%ans==) {
dfs(,,);
if(ok) break;
}
if(ok)
printf("%d\n",ans);
else printf("%d\n",total);
}
} int main()
{
solve();
return ;
}
POJ1011 Sticks的更多相关文章
- poj1011 Sticks (搜索经典好题)
poj1011 Sticks 题目连接: poj1011 Description George took sticks of the same length and cut them randomly ...
- poj1011 Sticks(dfs+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110416 Accepted: 25331 Descrip ...
- poj1011 Sticks(DFS+剪枝)
题目链接 http://poj.org/problem?id=1011 题意 输入n根棍子的长度,将这n根棍子组合成若干根长度相同的棍子,求组合后的棍子的最小长度.这题是poj2362的加强版,思路与 ...
- poj1011 Sticks[剪枝题]
https://vjudge.net/problem/POJ-1011 此题很重要.★★★ 很欢(e)乐(xin)的一道搜索剪枝题..poj数据还是太水了,我后来想不出来剪枝方法了,就加了句掐了时间语 ...
- poj-1011 sticks(搜索题)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- poj1011 Sticks (dfs剪枝)
[题目描述] George took sticks of the same length and cut them randomly until all parts became at most 50 ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
- 【poj1011】 Sticks
http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...
- POJ-1011(sticks,深搜)
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
随机推荐
- Flash剪贴板功能
做JS的都知道,如果不考虑浏览器的兼容问题,其实,JS本身的window.clipboardData对象是可以做到复制内容到剪贴板的功能,但除了IE浏览器,FF和Chrome浏览器都不支持.现在为了浏 ...
- Android应用程序签名详解 简介
转自: http://blog.csdn.net/lyq8479/article/details/6401093 本文主要讲解Android应用程序签名相关的理论知识,包括:什么是签名.为什么要给应用 ...
- EventBus (三) 源码解析 带你深入理解EventBus
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...
- vuejs过滤器
结合管道符 | {{messageOne | capitalize}} capitalize 首字母大写 {{messageOne | uppercase}} uppercase 大写字母 {{mes ...
- final-----finalize----finally---区别
一.性质不同 (1)final为关键字: (2)finalize()为方法: (3)finally为为区块标志,用于try语句中: 二.作用 (1)final为用于标识常量的关键字,final标识的关 ...
- onmeasure
UNSPECIFIE : 0 [0x0],未加规定的,表示没有给子view添加任何规定. EXACTLY : 1073741824 [0x40000000],精确的,表示父view为子view确定精确 ...
- WorldWind源码剖析系列:BMNG类构造函数深入分析
BMNG构造函数深入分析 一.主要类图 二.主要功能: 1) BMNG类 BMNG类将包含以“Blue Marble”为主题的所有可渲染影像的根节点添加到当前星球的可渲染对象列表中,包括 ...
- matlab中textread
今天打算跑下程序,突然发现,真的很烂,不会读入数据,简单的Iris.txt一上午都没读进去,在此对matlab中的textread函数做下总结,textscan函数待续. 本文主要内容引自http:/ ...
- 实践:VIM深入研究(20135301 && 20135337)
目录 一.基本知识 1.vim模式介绍 2.三种常用模式的切换 二.Vim文档编辑 1.vim重复命令 2.游标的快速跳转 3.复制粘贴和剪切 4.删除文本 5.字符的替换及撤销(Undo操作) 6. ...
- Spring 集成 Hibernate 和 Struts 2
在Spring中集成Hibernate,实际上就是将Hibernate中用到的数据源DataSource. SessionFactory实例(通常使用Hibernate访问数据库时,应用程序会先创建S ...