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  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- + Hi+)/ - , for all < i < N
HN = B
Hi >= , for all <= i <= N The sample garland with lamps that is shown on the picture has A = and B = 9.75.

Input

The input file consists of a single line with two numbers N and A separated by a space. N ( <= N <= ) is an integer representing the number of lamps in the garland, A ( <= A <= ) 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

 532.81

Sample Output

446113.34

Source

 
根据所有 Hi >= 0 这个条件进行二分枚举第二个点的值,最后计算出最后一个点的值
判断  if(num[i]<zero) 时,刚开始直接<0,导致错误,后来用了高精度zero(#define zero 1e-10),所以细节很重要
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define inf 1e12
#define N 1006
#define zero 1e-10
int n;
double A;
double num[N];
bool solve(double mid){
num[]=mid;
for(int i=;i<n;i++){
num[i]=*num[i-]+-num[i-];
if(num[i]<zero){
return false;
}
}
return true;
}
int main()
{ while(scanf("%d%lf",&n,&A)==){
num[]=A;
double low=-inf;
double high=inf;
for(int i=;i<;i++){
double mid=(low+high)/;
if(solve(mid)){
high=mid;
}
else{
low=mid;
}
}
printf("%.2lf\n",num[n-]);
}
return ;
}

poj 1759 Garland (二分搜索之其他)的更多相关文章

  1. POJ 1759 Garland(二分+数学递归+坑精度)

    POJ 1759 Garland  这个题wa了27次,忘了用一个数来储存f[n-1],每次由于二分都会改变f[n-1]的值,得到的有的值不精确,直接输出f[n-1]肯定有问题. 这个题用c++交可以 ...

  2. POJ 1759 Garland(二分答案)

    [题目链接] http://poj.org/problem?id=1759 [题目大意] 有n个数字H,H[i]=(H[i-1]+H[i+1])/2-1,已知H[1],求最大H[n], 使得所有的H均 ...

  3. poj 1759 Garland

    Garland Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2365   Accepted: 1007 Descripti ...

  4. Divide and conquer:Garland(POJ 1759)

     挂彩灯 题目大意:就是要布场的时候需要挂彩灯,彩灯挂的高度满足: H1 = A Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N HN = B Hi ...

  5. poj 1759 二分搜索

    题意:N个等差数列,初项X_i,末项Y_i,公差Z_i,求出现奇数次的数? 思路: 因为只有一个数出现的次数为奇数个 假设 第二个数字的个数为 奇数个,其余全部都是偶数个 ,累计出现的次数 a1偶数 ...

  6. POJ 1759

    Garland Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1236   Accepted: 547 Descriptio ...

  7. poj 1759(二分)

    传送门:Problem 1759 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有N个彩灯关在同一条绳上,给出第一个彩灯的高度A,并给 ...

  8. POJ 3579 median 二分搜索,中位数 难度:3

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3866   Accepted: 1130 Descriptio ...

  9. POJ River Hopscotch 二分搜索

    Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully ...

随机推荐

  1. python RabbitMQ队列使用(入门篇)

    ---恢复内容开始--- python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种que ...

  2. 小米路由器mini建FTP

    输入命令 df -h 复制代码 查看自己的分区路径,我自己用了一个闲置笔记本电脑硬盘转USB,插到mini,有四个区 为了方便,我选择把ftp安装在第一个分区  /extdisks/sda1/ 创建f ...

  3. javascript实现的手风琴折叠菜单效果

    演示地址:http://codepen.io/anon/pen/pJERMq 实现效果: HTML代码: <!DOCTYPE html> <html lang="en&qu ...

  4. js计算日期天数差-2013-9-26

    function daymath(sdate, edate) {             var startdate = sdate;             var enddate = edate; ...

  5. OpenWrt sscanf问题之于MT7620N与AR9341

    在MT7620N平台做好了wifidog的相关调试工作,除了eth驱动.wireless性能问题,其余的都能够基本正常. 依据实际须要要对已完毕的工作在AR9341平台上实现. 事实上也简单.基本功能 ...

  6. jQuery.extend 和 jQuery.fn.extend

    1.jQuery.extend 我们先把jQuery看成了一个类,这样好理解一些.jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑 ...

  7. [转]CSS目标伪类E:target

    CSS3 target 伪类不得不说那些事儿(纯CSS实现tab切换) 是不是觉得target有点眼熟?! 今天要讲的不是HTML的<a>标签里面有个target属性. target伪类是 ...

  8. 如何禁用不需要的HTTP方法

    IIS7.0默认开启了不安全的OPTIONS和TRACE方法,建议关闭这两个方法. 以下环境为windows server 2008.IIS7.0 方法(1):web.config 在<conf ...

  9. Protobuf, understand the hood

    proto文件定义 package lm; message Foo{ required int32 id = 1; } message Bar{ required int32 id = 1 [defa ...

  10. 【android】android下的junit

    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPa ...