각 위치 \((x,y)\)에서 물의 밀도는 \(\rho=1/y\)이고, 그 지점에서 단위 거리를 이동하는 데 걸리는 시간이 \(\rho\)초이므로(즉 속력 \(y\)), 임의의 경로 \(\gamma\)를 따라 이동하는 총 시간은
\[ \int_{\gamma} \frac{ds}{y} \]가 된다. 이 값이 최소가 되도록 하는 경로의 최소 시간을 구하면 된다.
문제 정보
문제 요약:
- 수영장 내부/변(단, \(y=0\)은 금지)에서 \((x_1,y_1)\to(x_2,y_2)\)로 이동한다.
- 위치 \((x,y)\)에서 단위거리 이동 시간은 \(1/y\)초이므로, 총 시간은 \(\int (ds/y)\).
- 각 쿼리(연습)마다 최소 시간을 출력한다. (오차 \(10^{-6}\) 허용)
제한 조건:
- 시간 제한: 1초
- 메모리 제한: 1024MB
- \(1 \le T \le 10{,}000\)
- \(-1000 \le x_1,x_2 \le 1000\)
- \(1 \le y_1,y_2 \le 1000\)
입출력 예제
입력 1:
| |
출력 1:
| |
핵심 관찰
관찰 1: 이 문제의 시간은 “상반평면 모델”의 쌍곡거리
유클리드에서의 미소 길이 \(ds=\sqrt{dx^2+dy^2}\)이므로,
\[ dt = \frac{ds}{y}=\frac{\sqrt{dx^2+dy^2}}{y} \]가 된다. 이는 상반평면(upper half-plane) \(\{(x,y)\mid y>0\}\)에서의 표준 쌍곡(하이퍼볼릭) 거리 요소와 동일하다.
따라서 \((x_1,y_1)\), \((x_2,y_2)\) 사이의 최소 시간은(=쌍곡거리)
\[ d = \operatorname{arcosh}\!\left(1+\frac{(x_1-x_2)^2+(y_1-y_2)^2}{2y_1y_2}\right) \]로 한 번에 계산된다.
관찰 2: \(\operatorname{arcosh}\)는 로그로 안정적으로 계산 가능
\[ \operatorname{arcosh}(z)=\ln\left(z+\sqrt{z-1}\sqrt{z+1}\right)\quad(z\ge 1) \]을 이용하면 표준 라이브러리 의존 없이도 안정적으로 구현할 수 있다.
알고리즘 설계 (Mermaid Flowchart)
flowchart TD
A["입력 T"] --> B["각 쿼리 (x1,y1,x2,y2) 읽기"]
B --> C["A = ((dx)^2+(dy)^2)/(2 y1 y2) 계산"]
C --> D["z = 1 + A"]
D --> E["ans = ln(z + sqrt(z-1)*sqrt(z+1))"]
E --> F["ans 출력 (오차 1e-6)"]
복잡도 분석
| 항목 | 복잡도 | 비고 |
|---|---|---|
| 시간 복잡도 | \(O(T)\) | 각 쿼리 \(O(1)\) |
| 공간 복잡도 | \(O(1)\) | 상수 메모리 |
코너 케이스 및 실수 포인트
| 케이스 | 설명 | 처리 방법 |
|---|---|---|
| 같은 점 | \((x_1,y_1)=(x_2,y_2)\) | \(\operatorname{arcosh}(1)=0\) |
| 부동소수점 오차 | 매우 작은 음수 출력 가능 | \(-10^{-12}\) 정도는 0으로 클램프 |
| 출력 오차 조건 | 상대/절대 \(1e-6\) | fixed + 적절한 setprecision |
구현 코드
C++
| |
참고 문헌 및 출처
- 백준 32231번: 재우의 삼수강
- 상반평면 모델의 쌍곡거리: \(d=\operatorname{arcosh}\!\left(1+\frac{|z-w|^2}{2\Im z\,\Im w}\right)\)
![Featured image of post [Algorithm] C++ 백준 32231번: 재우의 삼수강](/post/algorithm/2025-12-20-boj-32231-jaewoos-third-retake-hyperbolic-geometry-cpp-solution/wordcloud_hu_9d90461133f6480c.png)
![[Algorithm] C++ 백준 13543번: 수열과 쿼리 2](/post/algorithm/2025-12-20-boj-13543-sequence-and-queries-2-implicit-treap-cpp-solution/wordcloud_hu_1e4eb6a3efec537a.png)
![[Algorithm] C++ 백준 17372번: 피보나치 수의 최대공약수의 합](/post/algorithm/2025-12-20-boj-17372-fibonacci-gcd-sum-cpp-solution/wordcloud_hu_94c5bd100eedf3e2.png)
![[Algorithm] C++ 백준 32231번: 재우의 삼수강](/post/algorithm/2025-12-20-boj-32231-jaewoos-third-retake-hyperbolic-geometry-cpp-solution/wordcloud_hu_6b5d9f28adedfd92.png)
![[Algorithm] C++ 백준 3752번: 최대공약수 행렬식](/post/algorithm/2025-12-20-boj-3752-gcd-matrix-determinant-cpp-solution/wordcloud_hu_1490cf2b4f293afc.png)
![[Algorithm] C++ 백준 1185번: 유럽여행](/post/algorithm/2025-12-19-boj-1185-europe-travel-mst-kruskal-cpp-solution/wordcloud_hu_a7973902548ef27b.png)
![[Algorithm] C++ 백준 17441번: 파리채 만들기](/post/algorithm/2025-12-19-boj-17441-fly-swatter-making-green-theorem-cpp-solution/wordcloud_hu_f4a8a47eafcd5a3b.png)
![[Algorithm] C++ 백준 14853번: 동전 던지기](/post/algorithm/2025-12-19-boj-14853-coin-tossing-cpp-solution/wordcloud_hu_38e38de527bab2db.png)
![[Algorithm] C++ 백준 20176번: Needle](/post/algorithm/2025-12-19-boj-20176-needle-cpp-solution/wordcloud_hu_d0eac9b197bd7f0d.png)
![[Algorithm] C++ 백준 16481번 원 전문가 진우 - 방접원과 내접원의 관계](/post/algorithm/2025-12-03-boj-16481-excircle-incircle-geometry-cpp-solution/wordcloud_hu_ee675f3393f22f00.png)
![[Algorithm] C++/Python 백준 12771번: Oil](/post/algorithm/2025-08-14-boj-12771-oil-maximum-extraction-slope-sweep/wordcloud_hu_8874e7bcb7c63c40.png)