AtCoder Beginner Contest 144 D - Water Bottle

https://atcoder.jp/contests/abc144/tasks/abc144_d

まず水をなみなみ入れておいてそれを傾けていったとき水の量がちょうど $X$ になる角度を求めればいい。どこまで傾ければいいかは二分探索によって求められる。二分探索するには傾けた角度に対してどのくらいの水が残るのかを知らなくてはならない。傾けた角度を $\theta$ としたときの水の量を求めよう。求め方は $\tan \theta < b/a$ を満たすかで変わる。これは水面がちょうど対角線の上に来るときの前後で求め方が変わるということである。条件を満たすなら以下の式になる。

f:id:pekempey:20191029011237p:plain:w400

条件を満たなさないなら以下の式になる。

f:id:pekempey:20191029011255p:plain:w400

以上でこの問題を解くことができた。式を見ればすぐわかるように逆正接関数で直にも解ける。



最初は二分探索書いたんだけど3ケースだけ落ちて戸惑ってた。誤差が厳しいのかと思って結局逆正接関数を使って解いたんだけど、誤差が厳しいわけがないし結局式が間違ってた。解説放送みたいに辺の長さが求まることに気づけば二分探索する気にはならないんだけど慣れでやってしまう。

図 https://github.com/pekempey/figure/tree/master/ABC144C_water_bottle



Binary Search. If we know the amount of the water remaining in the bottle when tilt theta, we can find the theta such that the amount becomes exactly X. Let's try to find the amount. First, we notice that there are two states, which switched by the condition tan theta < b/a. If this condition is satisfied, the amount can be expressed by the following formula.

f:id:pekempey:20191029011237p:plain:w400

If this condition isn't satisfied, the amount can be expressed by the following formula.

f:id:pekempey:20191029011255p:plain:w400

So we have solved this problem. By the way, where does this condition come from? Actually, tan theta = b/a means the water surface is just on the diagonal line.