题目链接:http://poj.org/problem?id=1759

Garland
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2477   Accepted: 1054

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

 
 
 
 
题解:
  错误思路:惯性思维,一上来就想二分答案,即B点。但问题是,知道了A、B点,怎么求出中间的点呢?首先递推是推不出来的,然后就尝试用递归,看能否“先前进再返回”地求出各点,结果还是不行。后来也大概得出结论,如果要求出各个点:1)要么能推导出关于A、B点的公式直接计算;2)要么是知道相邻两个点的值,然后一路递推。公式我是推导不出来的,所以就要尝试第二种方法。所以:
1.二分第二个点,然后一路递推,直到求出B。
2.根据:H[i] = 2*H[i-1] + 2 - H[i-2] 可知,当H[i-2]固定时(对应A点已知),H[i-1]越小(对应第二个点) H[i]的值也越小。然后一直递推,最终B的值也越小。所以二分的第二个点B点具有同增同减性。
   —— 然而这个证明很牵强,因为H[i+1]越小时,应该是H[i]尽可能小, H[i-1]尽可能大。但此时H[i]、H[i-1]都是尽可能小,所以并不能说明:在第二个点越小的情况下,H[i+1]也越小,同理B点。所以也无法说明第二个点与B点具有同增同减性,那怎么证明呢?如下:
可知:H[3] = 2*H[2] + 2 - H[1] 
那么:H[4] = 2*H[3] + 2 - H[2]
得出:H[4] = 3*H[2] +6 - 2*H[1]。
一直将H[i]的式子带入H[i+1]的式子,那么得到:H[i+1] = a*H[2] + b - c*H[1], 其中a和b和c为正数。所以H[i+1]的增减性就显而易见了,因为H[1]已经确定,根据一元一方方程的特性,H[2](二分的第二个点)的值越小, H[i+1]的值也越小。所以表明了第二个点与所有点具有相同的增减性。所以,第二个点的值越小,B的值也越小。
  —— 或者,还有一个更快“目测”方法。观察:H[i] = 2*H[i-1] + 2 - H[i-2] 。 H[i-1] 的系数为2, H[i-2]的系数为1,所以H[i-1]占H[i]的权重最大,所以就可以得出:H[i] 与 H[i-1] 同增同减,一路递推。所以第二个点与B点具有同增同减性。
 
 
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+; int n;
double A, ans; bool test(double x1, double x2)
{
for(int i = ; i<=n; i++) //递推出每个点的高度
{
double x3 = *x2+-x1;
if(x3<=) return false; //出现负数,证明接地了, 不符合。
x1 = x2, x2 = x3;
}
ans = x2; //符合条件, 则更新答案。
return true;
} int main()
{
while(scanf("%d%lf", &n, &A)!=EOF)
{
double l = , r = A; //二分第二个点
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(A, mid))
r = mid - EPS;
else
l = mid + EPS;
}
printf("%.2f\n", ans);
}
}

POJ1759 Garland —— 二分的更多相关文章

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

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

  2. URAL 1066 Garland 二分

    二分H2的位置,判断条件为是否有Hi < 0 #include <cstdio> #include <cstring> #include <cstdlib> ...

  3. 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均 ...

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

    Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...

  5. poj 1759 Garland

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

  6. POJ-1759 Garland---二分+数学

    题目链接: https://cn.vjudge.net/problem/POJ-1759 题目大意: N个灯泡离地H_i,满足H1 = A ,Hi = (Hi-1 + Hi+1)/2 – 1,HN = ...

  7. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  8. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  9. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

随机推荐

  1. angularjs bootstrap 简介

    1.angular特色 数据双向绑定 模块化  var myapp=angular.module... 控制器  myapp.controller(){} 2.bootstrap特色 栅格结构  cs ...

  2. 区间翻转(codevs 3243)

    题目描述 Description 给出N个数,要求做M次区间翻转(如1 2 3 4变成4 3 2 1),求出最后的序列 输入描述 Input Description 第一行一个数N,下一行N个数表示原 ...

  3. 16.1114 模拟考试T1

    1.正确答案 [题目描述] 小H与小Y刚刚参加完UOIP外卡组的初赛,就迫不及待的跑出考场对答案. “吔,我的答案和你都不一样!”,小Y说道,”我们去找神犇们问答案吧”. 外卡组试卷中共有m道判断题, ...

  4. msp430项目编程47

    msp430综合项目---有线采集传输平台系统47 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  5. git status检测不到文件变化

    SourceTree(Git)无法检测新增文件的解决方法 有时候使用git管理软件SourceTree会遇到往项目里新增了文件,软件却没有任何反应的问题,这多发生在git合并出错而只能重新git的情况 ...

  6. 王垠 :写给支持和反对《完全用Linux工作》的人们 ( 2004/8/26)

    在一阵阵唾骂和欢呼声中,<完全用linux工作>被转载到了天涯海角.很多人支持我,也有很多人唾骂我.我不知道它是怎样流传到那么多地方,其实 我不知道它都被转到哪里去了…… 我知道现在这篇文 ...

  7. JavaScript对列表节点的操作:删除指定节点、删除最后一个节点、删除第一个节点、删除所有节点、增加节点

    使用菜鸟的运行环境直接测试:http://www.runoob.com/try/try.php?filename=tryjs_events <!DOCTYPE html> <html ...

  8. C# UserControl 判断是否是设计模式中

    In Windows Forms application, we can use Control.IsInDesignMode or LicenseManager.UsageMode == Licen ...

  9. time machine不备份指定文件夹

    osx中常常会使用timemachine来备份一些文件,timemachine能够使某个文件夹恢复到之前某个时刻的状态,很的方便.但是备份须要空间,特别是有些我们并不想备份一些无关紧要的文件,比方电影 ...

  10. AnimalWindow使用,实现界面动态消失

    http://m.blog.csdn.net/blog/shufac/24932279 http://blog.sina.com.cn/s/blog_455245fc01000a42.html Ani ...