Sticks//拼火柴//Central Europe 1995//dfs

2023-10-28 04:08

本文主要是介绍Sticks//拼火柴//Central Europe 1995//dfs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Sticks//拼火柴//POJ - 1011//dfs


题目

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
大意
某脑残把等长火柴棍砍断让你还原,求原等长火柴的长度。
网页链接:https://vjudge.net/contest/347799#problem/E(Central Europe 1995)

思路

答案一定是总长的因子,从小到大遍历其因子,其对应的另一个因子是分成的组数,不超过题目给定的段数,有合适的因子就是最小合适的。对于每一个因子,通过贪心+回溯来组合出适合的数。组数达到目标作为判断是否可行的标准,不可行则回溯。

代码

#include <iostream>//以组数收齐为判断标准即可
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;int n,t[100],vis[100];//vis用于判断是否用过,0没用过
bool judge;
int sum;
void dfs(int len,int sumn,int gpn,int gpa,int temp)
{if(gpn==gpa)//所有的组都收集齐了{judge=true;return;}if(sumn==len) //收集齐了一组{dfs(len,0,gpn+1,gpa,0);}for(int i=temp;i<n&&!judge;i++)//有一条线成功后就不需要继续{if(!vis[i]&&t[i]+sumn<=len){vis[i]=1;dfs(len,sumn+t[i],gpn,gpa,i);if(judge) return;vis[i]=0;if(sumn==0||sumn+t[i]==len) return ;//剪枝}}
}
bool judgen(int len,int gp)
{memset(vis,0,sizeof vis);judge=false;dfs(len,0,0,gp,0);return judge;
}
bool cmp(int a,int b)
{return a>b;
}
int main()
{while(1){cin>>n;if(n==0) break;sum=0;for(int i=0;i<n;i++) {cin>>t[i];sum+=t[i];}sort(t,t+n,cmp);int ans=sum;for(int i=n;i>1;i--)if(sum%i==0&&t[n-1]<=sum/i&&judgen(sum/i,i))//从小往大{ans=sum/i;break;}cout<<ans<<endl;}return 0;
}

注意

如果没有回溯会导致一些情况没有考虑完全
纯贪心特殊数据:
9
15 11 8 8 8 4 3 2 1
长度×组数即为总长
有符合的情况直接跳过剩余的任何情况

这篇关于Sticks//拼火柴//Central Europe 1995//dfs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/290581

相关文章

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

深度优先(DFS)和广度优先(BFS)——算法

深度优先 深度优先搜索算法(英语:Depth-First-Search,DFS)是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支,当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访

火柴游戏java版

代码 /*** 火柴游戏* <p>* <li>有24根火柴</li>* <li>组成 A + B = C 等式</li>* <li>总共有多少种适合方式?</li>* <br>* <h>分析:</h>* <li>除去"+"、"="四根,最多可用火柴根数20根。</li>* <li>全部用两根组合成"1",最大数值为1111。使用枚举法,A和B范围在0~1111,C为A+B。判断</li>** @

nyoj99(并查集+欧拉路+dfs)

单词拼接 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串。 前一个单词的结尾应该与下一个单词的道字母相同。 如 aloha dog arachnid gopher tiger rat   可以拼接成:aloha.arachnid.dog.gopher.rat.tiger 输入 第一行是一个整

【UVA】10003-Cutting Sticks(动态规划、矩阵链乘)

一道动态规划题,不过似乎可以用回溯水过去,回溯的话效率很烂的。 13988658 10003 Cutting Sticks Accepted C++ 1.882 2014-08-04 09:26:49 AC代码: #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include

【CF】E. Anya and Cubes(双向DFS)

根据题意的话每次递归分3种情况 一共最多25个数,时间复杂度为3^25,太大了 我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果 之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以 时间复杂度降低为 3 ^ (n/2+1) 题目链接:http://codeforces.com/contest/525/problem/E #include<set