kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:x: test/yukicoder/306/main.golden.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/306"
#define ERROR 1e-6

#include <cmath>
#include <iomanip>
#include <iostream>
#include "Mylib/Algorithm/golden_section_search_downwards.cpp"

namespace hl = haar_lib;

int main() {
  int xa, xb, ya, yb;
  std::cin >> xa >> ya >> xb >> yb;

  auto f =
      [&](long double p) {
        return std::hypot(xa, ya - p) + std::hypot(xb, yb - p);
      };

  auto ans = hl::golden_section_search_downwards<long double>(-1000, 1000, f);

  std::cout << std::fixed << std::setprecision(12) << ans << std::endl;

  return 0;
}
#line 1 "test/yukicoder/306/main.golden.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/306"
#define ERROR 1e-6

#include <cmath>
#include <iomanip>
#include <iostream>
#line 3 "Mylib/Algorithm/golden_section_search_downwards.cpp"
#include <functional>

namespace haar_lib {
  template <typename T = double, typename Func = std::function<T(T)>>
  T golden_section_search_downwards(T lb, T ub, const Func &f, int LOOP_COUNT = 100) {
    static const T phi = (1.0 + std::sqrt(5)) / 2;

    T t1 = 0, t2 = 0;

    while (LOOP_COUNT--) {
      t1 = (lb * phi + ub) / (phi + 1.0);
      t2 = (lb + ub * phi) / (phi + 1.0);

      if (f(t1) < f(t2)) {
        ub = t2;
      } else {
        lb = t1;
      }
    }

    return lb;
  }
}  // namespace haar_lib
#line 8 "test/yukicoder/306/main.golden.test.cpp"

namespace hl = haar_lib;

int main() {
  int xa, xb, ya, yb;
  std::cin >> xa >> ya >> xb >> yb;

  auto f =
      [&](long double p) {
        return std::hypot(xa, ya - p) + std::hypot(xb, yb - p);
      };

  auto ans = hl::golden_section_search_downwards<long double>(-1000, 1000, f);

  std::cout << std::fixed << std::setprecision(12) << ans << std::endl;

  return 0;
}
Back to top page