Wednesday, February 19, 2020

POJ.1759 Garland

1.Problem
http://poj.org/problem?id=1759

2.Idea
Binary_Search by 0<=h[2]<=INF.
h[n+1] = 2 * h[n] - h[n-1] + 2

3.Source
 int N;  
 double A;  
 double B = 1000000000.0;  
 bool C(double x)  
 {  
      double a3, a2, a1;  
      a2 = x;  
      a1 = A;  
      for (int i=0; i < N - 2; i++) {  
           a3 = 2.0 * a2 - a1 + 2.0;  
           if (a3 < 0) return false;  
           a1 = a2;  
           a2 = a3;            
      }  
      if (a2 < B) B = a2;  
      return true;  
 }  
 void solve()  
 {  
      double r = A;  
      double l = 0.0;  
      for(int i=0;i<100;i++) {  
           double m = (l + r) * 0.5;  
           if (C(m)) {  
                r = m;  
           }  
           else {  
                l = m;  
           }  
      }  
      cout << fixed << std::setprecision(2) << B << endl;  
 }  
 int main()  
 {  
      cin >> N >> A;  
      solve();  
      return 0;  
 }  

No comments:

Post a Comment