Sticks(Central Europe 1995) (DFS)
Sticks(Central Europe 1995)
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
Output
The output should contains the smallest possible length of original sticks, one per line.
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
6
5
题目简单翻译:
给你一个数n:
然后给你n个小木棍,把他们分成若干组,每组的总长度相等,求最短的总长度,使每组的总长度相等。
思路:
dfs,剪枝
把它分成若干组,从大到小搜索,使它们成为一组,直到搜到结果。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n,t[],vis[];
bool Get_Ans;
int Sum;
void dfs(int Length,int Now_Sum,int Now_Groups,int Aim_Groups,int Now_Position)
{
if(Now_Groups==Aim_Groups)//所有的组都收集齐了
{
Get_Ans=true;
return;
}
if(Now_Sum==Length) //收集齐了一组
{
dfs(Length,,Now_Groups+,Aim_Groups,);
}
for(int i=Now_Position;i<n&&!Get_Ans;i++)
{
if(!vis[i]&&t[i]+Now_Sum<=Length)
{
vis[i]=;
dfs(Length,Now_Sum+t[i],Now_Groups,Aim_Groups,i);
if(Get_Ans) return;
vis[i]=;
if(Now_Sum==||Now_Sum+t[i]==Length) return ;//剪枝
}
}
}
bool judge(int Length,int Groups)
{
memset(vis,,sizeof vis);
Get_Ans=false;
dfs(Length,,,Groups,);
return Get_Ans;
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
Sum=;
for(int i=;i<n;i++) scanf("%d",&t[i]),Sum+=t[i];
sort(t,t+n,cmp);
int Ans=Sum;
for(int i=n;i>;i--)
if(Sum%i==&&t[n-]<=Sum/i&&judge(Sum/i,i))
{
Ans=Sum/i;
break;
}
printf("%d\n",Ans);
}
return ;
}
Sticks(Central Europe 1995) (DFS)的更多相关文章
- ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków
ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...
- Sticks(UVA - 307)【DFS+剪枝】
Sticks(UVA - 307) 题目链接 算法 DFS+剪枝 1.这道题题意就是说原本有一些等长的木棍,后来把它们切割,切割成一个个最长为50单位长度的小木棍,现在想让你把它们组合成一个个等长的大 ...
- 2017-2018 ACM-ICPC, Central Europe Regional Contest (CERC 17)
A. Assignment Algorithm 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string ...
- XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--J.Cave
给你一棵树,现在有m个专家,每个专家计划从$a_i$走到$b_i$, 经过的距离不超过$d_i$,现在让你找一个点,使得所有专家的路途都能经过这个点 令$S_i$表示满足第i个专家的所有点,先检查1可 ...
- Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)
题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...
- Gym 101142G : Gangsters in Central City(DFS序+LCA+set)
题意:现在有一棵树,1号节点是水源,叶子节点是村庄,现在有些怪兽会占领一些村庄(即只占领叶子节点),现在要割去一些边,使得怪兽到不了水源.给出怪兽占领和离开的情况,现在要割每次回答最小的割,使得怪兽不 ...
- poj(1011)——Sticks(经典的dfs+剪枝)
题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. ...
- Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...
- Central Europe Regional Contest 2012 Problem c: Chemist’s vows
字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: #include<iostream> #include<string> ...
随机推荐
- Nasty Hacks <入门练手题>
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- iOS基本的发短信和打电话调用
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- linux crond服务
linux crond服务 linux crond服务简介:定时执行系统命令 查看crond服务状态:[root@www ~]# /sbin/service crond status 启动.停止.重启 ...
- 10个经典的Android开源项目(附源码包)
最近在抽空学习Android系统开发,对Android学习也比较感兴趣,刚开始学就试着在网上找几个项目源码研究看下,以下就将找到的Android项目源码列出,希望对正在或准备学习Android系统开发 ...
- 提高效率的便签By番茄时间管理 win7标签,小功能,大作用
今日待办 把一些重要的事情,列入其中. 着重处理. 活动清单 罗列一些最近需要做的事情,不一定按照紧急重要的程度. 把活动清单中的事情,按照实际情况,安排到今日待办当中. 还有一个我喜欢的'头脑风暴' ...
- 深入浅出Node.js (6) - 理解Buffer
6.1 Buffer结构 6.1.1 模块结构 6.1.2 Buffer对象 6.1.3 Buffer内存分配 6.2 Buffer的转换 6.2.1 字符串转Buffer 6.2.2 Buffer转 ...
- bzoj1379 [Baltic2001]Postman
Description 邮递员每天给N个村子的人送信,每个村子可能在某个十字路口上,或一条路的中央. 村子里的人都希望早点收到信,因此与邮递员达成一个协议:每个村子都有一个期望值Wi,如果这个村子是邮 ...
- [转]jQuery EasyUI 扩展-- 主题(Themes)
主题(Themes)允许您改变站点的外观和感观.使用主题可以节省设计的时间,让您腾出更多的时间进行开发.您也可以创建一个已有主题的子主题. 主题生成器(Theme Builder) jQuery UI ...
- poj 2342 Anniversary party_经典树状dp
题意:Ural大学有n个职员,1~N编号,他们有从属关系,就是说他们关系就像一棵树,父节点就是子节点的直接上司,每个职员有一个快乐指数,现在要开会,职员和职员的直接上司不能同时开会,问怎才能使开会的快 ...
- Js操作Select
jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selecto ...