Egg Drop

嵌入式Linux开发:手把手教你调试OV9281摄像头Sensor(基于全志平台DTS配置) 本文详细解析了嵌入式Linux开发中OV9281摄像头Sensor的调试全流程,重点介绍了基于全志平台的DTS配置指南。从硬件信号检测到软件驱动配置,包括设备树关键参数设置、内核驱动适配实战及图像质量调优技巧,为开发者提供了一套完整的调试方案,助力工业视觉和边缘计算设备的开发。 阅读详情

http://code.google.com/codejam/contest/dashboard?c=agxjb2RlamFtLXByb2RyEAsSCGNvbnRlc3RzGIP6AQw#s=p2

 

Problem

Imagine that you are in a building with F floors (starting at floor 1, the lowest floor), and you have a large number of identical eggs, each in its own identical protective container. For each floor in the building, you want to know whether or not an egg dropped from that floor will break. If an egg breaks when dropped from floor i, then all eggs are guaranteed to break when dropped from any floor j ≥ i. Likewise, if an egg doesn't break when dropped from floor i, then all eggs are guaranteed to never break when dropped from any floor j ≤ i.

We can define Solvable(F, D, B) to be true if and only if there exists an algorithm to determine whether or not an egg will break when dropped from any floor of a building with F floors, with the following restrictions: you may drop a maximum of D eggs (one at a time, from any floors of your choosing), and you may break a maximum of B eggs. You can assume you have at least D eggs in your possession.

Input

The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as:

F D B

Solvable(F, D, B) is guaranteed to be true for all input cases.

Output

For each test case, output one line containing "Case #x: " followed by three space-separated integers: Fmax, Dmin, and Bmin. The definitions are as follows:

  • Fmax is defined as the largest value of F' such that Solvable(F', D, B) is true, or -1 if this value would be greater than or equal to 232 (4294967296).
    (In other words, Fmax = -1 if and only if Solvable(232, D, B) is true.)
  • Dmin is defined as the smallest value of D' such that Solvable(F, D', B) is true.
  • Bmin is defined as the smallest value of B' such that Solvable(F, D, B') is true.

Limits

1 ≤ N ≤ 100.

Small dataset

1 ≤ F ≤ 100,
1 ≤ D ≤ 100,
1 ≤ B ≤ 100.

Large dataset

1 ≤ F ≤ 2000000000,
1 ≤ D ≤ 2000000000,
1 ≤ B ≤ 2000000000.

 

这个题目如果一个一个去想,去算的话,估计就糊涂了,其实最主要的是递推公式.

假设F(D,B)表示,D个蛋,可以破B个最高可以测试的楼层数.那么我们去测试的时候,第一个蛋应该放在哪里呢?

可以这样想,如果第一个蛋破了,省下的蛋应该可以把第一个蛋破的那个楼层以下的都检验一遍,也就是F(D-1, B-1)+1

这就是第一个蛋扔的位置,如果从这个位置扔下去蛋不破,那么在这个基础上,还可以测试F(D-1, B)个楼层.

因此F(D, B) = F(D-1, B)+F(D-1, B-1)+1

 

有了递推公式,后面的事情就好办了, 知道D,B,求F,直接递归就行.

有了F,D求B,或者有了F,B求D, 就从小到大遍历B(D),直到当前解满足.

 

下面考虑的就是效率的问题,由于直接递归是O(2^n)时间和空间复杂度的, 所以对于大数,需要做一些优化处理.

首先,计算出一些常用的递归出口.

F(D, D)=2^D-1;

F(D, 1)=D;

F(D, 2)=D*(D+1)/2;

F(D, 3)=(D-1)*D*(D+1)/6+D;

由于题目要求的最大F限制是2^32, 比这个大的都返回-1就可以了.

所以当D和B很大的时候,基本上都是-1, B<=3的时候直接用公式解, B>3的时候, 我们判断D是否足够小似的F(D, 3)<2^32, 否则直接返回-1, 这样就似的很深的递归不会出现.

 

另外, 由于小于2^32的都要返回结果,所以这里采用unsigned long来储存计算结果, 用返回值来控制是否溢出.

 

代码如下:

#include <stdio.h>


#define MAX_VALUE (4294967296UL)

int GetSum(unsigned int nEgg, unsigned int nBreak, unsigned long* nSum)
{
 unsigned long nSum1, nSum2;
 if(nEgg==nBreak)
 {
  if(nEgg>32) return (-1);
  else if(nEgg == 32)
  {
   *nSum = 0xFFFFFFFF;
  }
  else
  {
   *nSum = ((1<<nEgg)-1);
  }
  return 0;
 }
 if(nBreak==3)
 {
  if(nEgg>2953) return (-1);
  else
  {
   unsigned long nAdd;
   nAdd = (nEgg-1)*nEgg/2;
   if(nAdd%3==0)
   {
    *nSum = (nAdd/3*(nEgg+1)+nEgg);
   }
   else
   {
    *nSum = ((nEgg+1)/3*nAdd+nEgg);
   }
   return 0;
  }
 }
 
 if(GetSum(nEgg-1, nBreak, &nSum2)==-1)
 {
  return -1;
 }
 if(GetSum(nEgg-1, nBreak-1, &nSum1)==-1)
 {
  return -1;
 }
 if(nSum1+nSum2+1<nSum1) return (-1);
 *nSum = nSum1+nSum2+1;
 return 0;
}

int GetTotalFloor(int nEgg, int nBreak, unsigned long* pSum)
{
 if(nBreak==1)
 {
  *pSum = nEgg;
  return 0;
 }
 if(nBreak==2)
 {
  if(nEgg>92681) return (-1);
  if(nEgg%2==0)
  {
   *pSum = (nEgg/2*(nEgg+1));
  }
  else
  {
   *pSum = ((nEgg+1)/2*nEgg);
  }
  return 0;
 }
 if(nBreak>=3)
 {
  if(nEgg>2953) return (-1);
 }
 return GetSum(nEgg, nBreak, pSum);
}

int main()
{
 int nRound;
 FILE* fp;
 int i, j;
 fp = fopen("1.txt", "r");
 fscanf(fp, "%d", &nRound);

 for(i=0; i<nRound; i++)
 {
  int nFloor, nEgg, nBreak;
  unsigned long nSum;
  int nRet;
  fscanf(fp, "%d %d %d", &nFloor, &nEgg, &nBreak);
  printf("Case #%d: ", i+1);
  nRet = GetTotalFloor(nEgg, nBreak, &nSum);
  if(nRet == -1) printf("-1 ");
  else printf("%u ", nSum);
  for(j=1; j<nEgg; j++)
  { 
   if(nBreak>j) nRet = GetTotalFloor(j, j, &nSum);
   else nRet = GetTotalFloor(j, nBreak, &nSum);
   if(nRet==-1 || nSum>=(unsigned)nFloor) break;
  }
  printf("%d ", j);

  for(j=1; j<nBreak; j++)
  {
   nRet = GetTotalFloor(nEgg, j, &nSum);
   if(nRet==-1 || nSum>=(unsigned)nFloor) break;
  }
  printf("%d/n", j);
 }
 return 0;
}

求无向图中所有的环 C++ python 求无向图中所有的环 阅读详情

相关推荐

飞牛fnNAS虚拟机标准安装

飞牛NAS是一款基于Debian开发的国产免费NAS系统,支持x86架构设备,可将闲置硬件改造为NAS服务器。系统安装简便,支持多种存储模式与硬盘休眠功能,具备智能影视管理、照片备份等实用功能,并提供多种远程访问方式。飞牛NAS还支持Docker扩展,目前已承诺永久免费。文章详细介绍了在VMware虚拟机中安装飞牛NAS的步骤,包括下载镜像、创建虚拟机、初始化设置等过程,并展示了创建共享文件夹等基本操作,帮助用户快速上手这款功能强大的免费NAS系统。

dgnankai的专栏 3197

Egg Drop 扔鸡蛋

假设有 n=2 个完全一样的鸡蛋, 有 k=36 层楼。 存在一个楼层 m,把鸡蛋从低于m的楼层扔下去的时候,鸡蛋不会摔破;把鸡蛋从高于等于m的楼层扔下去的时候,鸡蛋会摔破。题目要求,找出临界楼层m,并且扔鸡蛋的次数要做到最少。 思路: 最简单的方法是,从第 1 层往上,一层一层 的扔鸡蛋,直到鸡蛋摔碎。这样需要扔鸡蛋 m 次。 下面用递归的方法解决该问题。假设鸡蛋从 x 层扔下去。(1)如

jiyanfeng1的专栏 2820

GameFramework框架详解之 Network网络框架

NetworkManager只负责维护NetworkChannel,实际的有种我们简历网络连接的也是NetworkChannel作为一个网路频道,这样管理起来比较方便,比如:一个游戏可能有多个网络连接,那么我们建立多个网络频道即可,或者当我们一个网络频道断线后,我们可以直接再新建一个,防止数据混乱。/// /// 网络管理器。/// /// 网络频道。/// 用户自定义网络错误事件。/// 检查是否存在网络频道。/// 获取网络频道。/// 获取所有网络频道。

TxNet 3310

1104: Egg Drop,1105: Excellence

明若清溪天下绝歌 缱绻成说,不知该在哪处着墨;一生情深怎奈何世事 徒留斑驳,只一念痴恋成奢。 题目描述 Suppose you are given an egg and a k-floor building, and you want to know the highest floor from which you can drop the egg and not have it ...

0k-ok 467

Egg Drop(水题)

DescriptionThere is a classic riddle where you are given two eggs and a k-floor building and you want to knowthe highest floor from which you can drop the egg and not have it break.It turns out that y...

Baiyi_destroyer的博客 730

The Egg-Drop Problem

直接从网上拿图片来了,简单方便 只要枚举第一次扔的楼层号,然后取最好的情形即可。 最后列出递推公式,本来想试着找通项公式的,暂时以失败告终。 不过,在找资料过程中,发现了另一种角度思考这个问题。 上面的思路是,n层楼m个蛋寻找最佳次数。 而现在的思路是,m个蛋扔k次,最多能判断多少层楼。 令f(m,k)为m个蛋扔k次最多能判断的楼层数目。 f(

8rfuz的博客 1269

code.jam - egg drops

    这是比较难的一道题目,要解决问题首先要摸清楚F,D,B之间的数学规律,这确实比较难并且不好描述,我想了很久才想出来,我觉得如果要求在一个小时内搞定还真是非常不容易,至少如果是我的话如果无法灵光一现的话还真是想不出来……。     首先尝试最简单的递归方法,但是深度太大以致于连小测试数据都无法搞定,只能用递推,F,D,B <= 100 范围内可以构造一个101×101的二维数组并递推填充,可

keelun , keep walking~~~ 767

【LeetCode 887】 Super Egg Drop

题目描述 You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped

iCode_girl的博客 194

LeetCode刷题:887. Super Egg Drop

LeetCode刷题:887. Super Egg Drop 原题链接:https://leetcode.com/problems/super-egg-drop/ You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is identical in functi...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 595

LeetCode //C - 887. Super Egg Drop

This article discusses the classic "dropping eggs" problem, aiming to determine the minimum number of trials required to find the critical floor f given k eggs and an n-story building. The key idea is dynamic programming: dp[e] is defined to represent the

Made in Code 664

887. Super Egg Drop

You are givenKeggs, and you have access to a building withNfloors from1toN. Each egg is identical in function, and if an egg breaks, you cannot drop itagain. You know that there exists a ...

weixin_30580341的博客 123

【算法】Super Egg Drop 鸡蛋掉落

【算法】Super Egg Drop 鸡蛋掉落题目解题思路代码实现 题目 You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is identical in function, and if an egg breaks, you cannot drop it agai...

无良剑&染的博客 767

Super Egg Drop

一道陈题. 100 层楼 2 个玻璃球 起因是窥室友手机屏, 看到他群里有人问一个经典问题. 两个一模一样的玻璃球, 两个玻璃球如果从一定高度掉落到地上会被摔碎, 如果在这个高度以下往下扔怎么都不会碎, 现在已知这个恰巧摔碎的高度范围在 1 层楼到 100 层楼之间, 如何用最少的试验次数, 用这两个玻璃球测试出玻璃球恰好摔碎的楼高呢?1 当只剩 1 个球时, 只能一层一层往上测试....

weixin_30496751的博客 310

LeetCode 887. Super Egg Drop

You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a f...

杂草园 1499

LeetCode 887 鸡蛋掉落 Super Egg Drop

题目链接 887. 鸡蛋掉落 AC代码 【传送门: Kiritow/OJ-Prolems-Source LeetCode-CN/887.cpp】 题目原文(翻译得极其生硬) 你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N  共有 N 层楼的建筑。 每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去。 你知道存在楼层 F ,满足 0 &lt;= F &lt;= N 任何从高于...

Kiritow的学园 2801

887. Super Egg Drop(Leetcode每日一题-2020.04.11)

占位

Bryan要加油 455

Geeks面试题: Egg Dropping Puzzle

Egg Dropping Puzzle The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors. Suppose that we wish to know which stories ...

架构设计 283

EggDrop Problem(扔鸡蛋问题)

一开始看不同的version描述很费解,后来看了evasean 的解释豁然开朗,都是类似于二分法的思想。或许升级可以问最少用多少个鸡蛋,扔几次之类的。 Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from...

weixin_38206454的博客 768

891. Super Egg Drop

You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a fl...

Do not mess it up 910

基于COMSOL的甲烷重整器内部加热管顺逆流加热技术及水汽交换反应模型 · COMSOL 2025版

内容概要:本文详细探讨了利用COMSOL软件进行甲烷重整器建模的方法和技术要点。首先介绍了甲烷与水蒸汽在催化剂表面上发生的重整反应及其动力学特性,强调了正确设置阿伦尼乌斯方程参数的重要性。接着讨论了不同类型的加热管布局(如顺流和逆流)对于温度场分布的影响,并提供了具体的边界条件设置实例。此外还涉及到了如何通过调整反应物浓度指数来提高模型准确性的问题。文中特别提到了物质传递与热传导之间的耦合关系以及如何避免由于单向耦合造成的错误结果。最后分享了一些实用技巧,例如使用'事件'功能防止过高的温度损害设备。适合人群:从事能源化工领域的研究人员、工程师,尤其是那些需要借助仿真工具优化甲烷重整工艺的人士。使用场景及目标:帮助读者掌握使用COMSOL建立高效、精确的甲烷重整器模型所需的知识和技能,从而更好地理解和改进实际工业生产中的相关装置性能。其他说明:文中不仅包含了理论知识讲解,还有大量具体操作指南和常见问题解决方案,是一份非常实用的技术参考资料。

SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料

SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料,个人经导师指导并认可通过的高分设计项目,评审分99分,代码完整确保可以运行,小白也可以亲自搞定,主要针对计算机相关专业的正在做毕业设计、大作业的学生和需要项目实战练习的学习者,可作为毕业设计、课程设计、期末大作业,代码资料完整,下载可用。SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据库+论文资料SpringBoot微服务架构下的区块链农产品溯源系统源码与数据

上一篇: Always Turn Left
下一篇: Google Code Jam预选赛完成.
fire_woods
博客等级 码龄24年 408粉丝 108原创
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值