题目链接:http://poj.org/problem?id=1759
Garland
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2477 | Accepted: 1054 |
Description
![](http://poj.org/images/1759_1.jpg)
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点具有同增同减性。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++) 13 #define ms(a,b) memset((a),(b),sizeof((a))) 14 using namespace std; 15 typedef long long LL; 16 const double EPS = 1e-8; 17 const int INF = 2e9; 18 const LL LNF = 9e18; 19 const int mod = 1e9+7; 20 const int maxn = 1e5+10; 21 22 int n; 23 double A, ans; 24 25 bool test(double x1, double x2) 26 { 27 for(int i = 3; i<=n; i++) //递推出每个点的高度 28 { 29 double x3 = 2*x2+2-x1; 30 if(x3<=0) return false; //出现负数,证明接地了, 不符合。 31 x1 = x2, x2 = x3; 32 } 33 ans = x2; //符合条件, 则更新答案。 34 return true; 35 } 36 37 int main() 38 { 39 while(scanf("%d%lf", &n, &A)!=EOF) 40 { 41 double l = 0, r = A; //二分第二个点 42 while(l+EPS<=r) 43 { 44 double mid = (l+r)/2; 45 if(test(A, mid)) 46 r = mid - EPS; 47 else 48 l = mid + EPS; 49 } 50 printf("%.2f ", ans); 51 } 52 }