POJ 1759
Garland
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1236 | Accepted: 547 |
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
二分答案,判断的时候设第二个数为0,依此递推后面n - 2个数,当第二个数加a 时,后面的数依次增加 i - 1,根据此规律可以判断。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define maxn 1005 9 #define eps 1e-3 10 11 int n; 12 double a; 13 double s[maxn]; 14 15 bool check(double x) { 16 if(x < s[n]) return false; 17 for(int i = 3; i <= n; ++i) { 18 if(s[i] + (x - s[n]) / ((n - 1)) * (i - 1) < 0) return false; 19 } 20 return true; 21 } 22 23 void solve() { 24 s[1] = a; 25 s[2] = 0; 26 double _min = 0; 27 for(int i = 3; i <= n; ++i) { 28 s[i] = (s[i - 1] + 1) * 2 - s[i - 2]; 29 _min = min(_min,s[i]); 30 } 31 32 //printf("s[n] = %f %f\n",s[n],-_min); 33 34 double l = 0,r = s[n] + (n - 1) * (_min < 0 ? -_min : 0); 35 //printf("r = %f\n",r); 36 37 while(r - l >= eps) { 38 39 double mid = (l + r) / 2; 40 //printf("%f\n",mid); 41 if(check(mid)) r = mid; 42 else l = mid; 43 } 44 45 printf("%.2f\n",l); 46 47 48 } 49 50 int main() 51 { 52 53 // freopen("sw.in","r",stdin); 54 scanf("%d%lf",&n,&a); 55 56 solve(); 57 return 0; 58 }