結果
問題 | No.306 さいたま2008 |
ユーザー |
![]() |
提出日時 | 2020-03-14 12:09:05 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1 ms / 2,000 ms |
コード長 | 1,268 bytes |
コンパイル時間 | 392 ms |
コンパイル使用メモリ | 51,200 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2025-02-14 12:16:11 |
合計ジャッジ時間 | 1,306 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:46:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 46 | scanf("%Lf %Lf %Lf %Lf", &xa, &ya, &xb, &yb); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
/*** @brief 黄金比分割探索* @author えびちゃん*/#ifndef H_ternary_search#define H_ternary_search#include <cmath>#include <utility>template <typename Fn, typename Tp>std::pair<Tp, Tp> optimize_convex(Fn&& f, Tp xl, Tp xu, Tp err, bool maximize = true) {// Returns {argmin f(x), min f(x)}.Tp const phi = (1 + std::sqrt(static_cast<Tp>(5))) / 2;int const iter = (std::log(xu-xl) - std::log(err)) / std::log(phi) + 1;Tp xml = (phi * xl + xu) / (1 + phi);Tp xmu = (xl + phi * xu) / (1 + phi);Tp yml = f(xml);Tp ymu = f(xmu);for (int i = 0; i < iter; ++i) {if (!maximize ^ (yml > ymu)) {xu = xmu;xmu = xml;ymu = yml;xml = (phi * xl + xu) / (1 + phi);yml = f(xml);} else {xl = xml;xml = xmu;yml = ymu;xmu = (xl + phi * xu) / (1 + phi);ymu = f(xmu);}}return std::make_pair(xml, yml);}#endif /* !defined(H_ternary_search) */#include <cstdio>#include <cmath>int main() {long double xa, ya, xb, yb;scanf("%Lf %Lf %Lf %Lf", &xa, &ya, &xb, &yb);auto f = [&](auto y) { return std::hypot(xa, y-ya) + std::hypot(xb, yb-y); };long double y = optimize_convex(f, 0.0L, 1e3L, 1e-6L, false).first;printf("%.20Lf\n", y);}