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> ...
随机推荐
- iOS数据持久化 -- Core Data-备用
Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ...
- 开心菜鸟学习系列学习笔记------------nodejs util公共函数
global 在最外层定义的变量: 全局对象的属性: 隐式定义的变量(未定义直接赋值的变量). 一.process process 是一个全局变量,即 global 对象的属性 ...
- MySQL导出数据文件
SELECT * INTO OUTFILE '/root/a.txt' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM t_log_in ...
- VS代码生成工具ReSharper使用手册:配置快捷键
原文 http://www.cnblogs.com/PHPIDE/archive/2013/05/16/3081783.html VS代码生成工具ReSharper提供了丰富的快捷键,可以极大地提高你 ...
- SQL查询最近三个月的数据(查询最近几天,几年等等)
定义和用法 :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId OrderPayD ...
- spring bean初始化和销毁
spring bean的创建与消亡由spring容器进行管理,除了使用<bean><property/></bean>进行简单的属性配置之外,spring支持更人性 ...
- Linux 挂载光驱
Linux的硬件设备都在/dev目录下,/dev/cdrom表示光驱,挂载方法如下: 1.挂载光驱 [root@oracle ~]# mount -t iso9660 /dev/cdrom /mnt/ ...
- Poj2946-The Warehouse(bfs+哈希)
题目我就不粘贴了... 题意:给出地图,最大8*8,出口用'E'表示,空地用'.'表示,数字表示此处有多少个箱子,主人公的起点应该是在有箱子的地方,他可以朝四个方向移动,但是只有两种方式 一种是他移动 ...
- [转] Hive 内置函数
原文见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.内置运算符1.1关系运算符 运算符 类型 说明 A ...
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...