POJ1759Garland题解

  • 题目
    • 链接
    • 字面描述
  • 思路
  • 重点
  • 代码实现

题目

链接

http://poj.org/problem?id=1759

字面描述

Garland
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4708 Accepted: 1976
Description

The New Year garland consists of N lamps attached to a common wire that hangs down on the ends to which outermost lamps are affixed. The wire sags under the weight of lamp in a particular way: each lamp is hanging at the height that is 1 millimeter lower than the average height of the two adjacent lamps.

The leftmost lamp in hanging at the height of A millimeters above the ground. You have to determine the lowest height B of the rightmost lamp so that no lamp in the garland lies on the ground though some of them may touch the ground.

You shall neglect the lamp’s size in this problem. By numbering the lamps with integers from 1 to N and denoting the ith lamp height in millimeters as Hi we derive the following equations:

H1 = A
Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N
HN = B
Hi >= 0, for all 1 <= i <= N

The sample garland with 8 lamps that is shown on the picture has A = 15 and B = 9.75.
Input

The input file consists of a single line with two numbers N and A separated by a space. N (3 <= N <= 1000) is an integer representing the number of lamps in the garland, A (10 <= A <= 1000) is a real number representing the height of the leftmost lamp above the ground in millimeters.
Output

Write to the output file the single real number B accurate to two digits to the right of the decimal point representing the lowest possible height of the rightmost lamp.
Sample Input

692 532.81
Sample Output

446113.34
Source

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nFgyD5Tk-1660137569306)(http://poj.org/images/1759_1.jpg#pic_center)]

思路

二分第二个灯的高度,根据a[i]=(a[i-1]+a[i+1])/2-1;=>a[i+1]=2a[i]-a[i-1]+2;=>h[i]=2h[i-1]-h[i-2]+2;往后退,若有接触地面则二分,否则记录答案再二分

重点

建议整个eps=1e-7,对精度进行优化

代码实现

#include<cstdio>
using namespace std;const int maxn=1e3+10;
const double eps=1e-7;
int n;
double ll,ans;
double a[maxn];
//模拟是否接触地面
inline bool check(double mid){a[2]=mid;for(int i=3;i<=n;i++){a[i]=2*a[i-1]-a[i-2]+2;if(a[i]<eps)return false;}ans=a[n];return true;
}
int main(){scanf("%d%lf",&n,&ll);a[1]=ll;double l=0.0,r=ll;//二分while(r-l>eps){double mid=(l+r)/2;if(!check(mid))l=mid;else r=mid;}printf("%.2lf\n",ans);return 0;
}

POJ1759Garland题解相关推荐

  1. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  2. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  3. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  4. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  5. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  6. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  7. [洛谷1383]高级打字机 题解

    题解 这道题一看就珂以用主席树啊 这是一道神奇的题目,那么我们先敲一个主席树,然后维护一个数组len,表示下一次应该在len + 1插入, 之后对于T操作,在上一个版本的len + 1上直接执行插入 ...

  8. luogu P1549 棋盘问题(2) 题解

    luogu P1549 棋盘问题(2) 题解 题目描述 在\(N * N\)的棋盘上\((1≤N≤10)\),填入\(1,2,-,N^2\)共\(N^2\)个数,使得任意两个相邻的数之和为素数. 例如 ...

  9. 【题解搬运】PAT_L1-009 N个数求和

    从我原来的博客上搬运.原先blog作废. (伪)水题+1,旨在继续摸清这个blog(囧 题目 就是求N个数字的和.麻烦的是,这些数字是以有理数"分子/分母"的形式给出的,你输出的和 ...

  10. 第五届合肥工业大学宣城校区程序设计大赛题解

    问题 A: 小问题 时间限制: 1 Sec  内存限制: 128 MB  Special Judge 题目描述 林喵喵特别喜欢解决女孩子们提出的问题. 于是, 有一天殷老师问了林喵喵一个小问题. 给出 ...

最新文章

  1. 双击“本地连接”打不开无反应的解决方法
  2. 全卷积(FCN)论文阅读笔记:Fully Convolutional Networks for Semantic Segmentation
  3. SpringBoot_配置-properties配置文件编码问题
  4. 【view桌面虚拟化系列】1-vSphere搭建
  5. LSTM公式详解推导
  6. 2017.9.7 骑士 思考记录
  7. C++基础——用C++实例理解UML类图
  8. 可信人工智能白皮书(概要)
  9. 【笔记+代码】网格搜索-GridSearchCV
  10. 简单循迹小车实验心得_简单循迹小车的制作
  11. python绘制emoji_在Python中将emoji转换为文本
  12. linux加载打印机驱动程序,Linux下安装HP打印机的驱动程序
  13. 软件架构模式之管道-过滤器模式--分析
  14. 微信分享appID注册一次后即绑定该应用?
  15. opencv vector matlab,opencv实现 Matlab 寻找峰值函数 findpeaks
  16. jdk-17下载与安装(window10)
  17. Chrome侧边栏书签插件 Candy Bookmarks Sidebar
  18. java中throw是什么意思_Java中throw和throws的区别是什么
  19. linux查询文件大小
  20. 记录:微信小程序Switch样式调整

热门文章

  1. 服务器端口怎么调出虚拟键盘,win10系统打开软键盘 win10怎么调出软键盘
  2. win7 命令行开启WiFi
  3. Android状态栏添加快捷开关(Tile)
  4. l计算机中数的表示教案,计算机一级B第一章教案汇总.doc
  5. simon haykin_Simon JavaScript游戏教程
  6. 删除win10 qaa输入法
  7. 详解浅拷贝,深拷贝及实现方法
  8. 大学计算机类专业详解,多伦多大学计算机相关专业解析
  9. JavaScript中的call(),apply(),伪数组转化为数组
  10. VC++界面编程之--实现工具栏自定义皮肤