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> ...
随机推荐
- hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结
最近刚开始接触hibernate+spring+mvc+Easyui框架,也是刚开通了博客,希望能记录一下自己实践出来的东西,让其他人少走弯路. 转让正题,以个人浅薄的认识hibernate对于开发人 ...
- sqlserver exists和in 与exists和not in
1.exists 和 in 1.1 正常情况下exists和in的效果是一样的,如图试验 即使子查询中包含null也没有关系,依然可以正常使用 1.2 in 和 exists效率比较 先看in 由图中 ...
- Nginx 内置变量,细化规则,真实IP获取及限制连接请求
希望下周测试之后能用起来!!!感觉很有用的. http://www.bzfshop.net/article/176.html http://www.cr173.com/html/19761_1.htm ...
- UDP数据接收服务器
简介 这是我在做一个要用UDP方式进行数据传输时,自己写的一个多线程的UDP数据接收服务器, 它能将接收到的UDP数据包存成文件,并提供数据包接收时间监测: 还支持键盘命令响应,以将数据写到新的文件, ...
- 2013第49周三IE9文档模式
今天完善了原有模块的代码和注释,然后继续之前新模块的开发,并写了两边的service接口,除了因为邮件中有部分问题让我分心外,专心下来写代码的感觉真好,今天基本上没遇到多少让我新感悟的技术问题,就总结 ...
- ASCII码、base64编码 为什么有的代码要用 base64 进行编码?
百度百科 ASCII码:http://baike.baidu.com/link?url=bNtzytBhlSUt_l3pwpfICxCxqgAfqsBMaeWX6QF7gH46Tg4pQtKM2aAV ...
- java 实现排序
package com.cjs.sort; /** * 此类用来提供针对整数的各种排序算法 * * @author S * @version 1.0 */ public class MySort { ...
- OpensStack instance debug
错误: 创建实例 "RuiyTest23" 失败: 请稍后再试 [错误: Virtual Interface creation failed].
- <转载>模板声明中template <typename T>和template <class T>
原文地址http://blog.csdn.net/bug07250432/article/details/10150625 在c++Template中很多地方都用到了typename与class这两个 ...
- 关于Redis
在同步dump.rdb文件时要执行service redis stop后,再拷贝目标rdb文件过去,然后再start 而不是拷贝目标rdb文件过去后直接执行restart 因为redis在执行sto ...