首页 技术 正文
技术 2022年11月18日
0 收藏 898 点赞 3,837 浏览 1758 个字

Description

poj 1759 Garland (二分搜索之其他)

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

Northeastern Europe 2000 根据所有 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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,035
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,521
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,369
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,150
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,783
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,864