POJ - 1759 Garland(二分)

来源:互联网 发布:生化危机6优化怎么样 编辑:程序博客网 时间:2024/05/18 00:24
Garland
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1928 Accepted: 809

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
题意:有N个在第一象限的点,给出第一个点的高度,每个点的高度满足公式Hi = (Hi-1 + Hi+1)/2 - 1,(1 < i < N),求最后一个点高度最小是多少
思路:很明显,当我第二个点越低,最后一个点也会越低,所以是单调函数,然后二分第二个点,记录最后一个点
#include <map>#include <set>#include <cmath>#include <ctime>#include <queue>#include <vector>#include <cctype>#include <cstdio>#include <string>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#include <functional>using namespace std;#define pb push_back#define mp make_pair#define fillchar(a, x) memset(a, x, sizeof(a))#define copy(a, b) memcpy(a, b, sizeof(a))#define lson rt << 1, l, mid#define rson rt << 1|1, mid + 1, rtypedef long long LL;typedef pair<int, int > PII;typedef unsigned long long uLL;template<typename T>void print(T* p, T* q, string Gap = " ") {int d = p < q ? 1 : -1;while(p != q) {cout << *p;p += d;if(p != q) cout << Gap;}cout << endl;}template<typename T>void print(const T &a, string bes = "") {int len = bes.length();if(len >= 2)cout << bes[0] << a << bes[1] << endl;else cout << a << endl;}const LL INF = 1e17;const int MAXM = 1e2 + 5;const int MAXN = 1e3 + 5;int N;double A, ans;double H[MAXN];bool C(double m){    H[1] = A;    H[2] = m;    for(int i = 2;i <= N;i ++){        if(H[i] < 0) return false;        H[i + 1] = (H[i] + 1) * 2. - H[i - 1];    }    ans = H[N];    return true;}int main(){    while(~scanf("%d%lf", &N, &A)){        double lb = -1, ub = INF * 1.;        for(int i = 0;i < 100;i ++){            double mid = (ub + lb) / 2.;            if(C(mid)){                ub = mid;            }            else{                lb = mid;            }        }        printf("%.2f\n", ans);    }    return 0;}


1 0