0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC 133: D - Rain Flows into Dams

0
Posted at

問題

D - Rain Flows into Dams

解法

山 $i$ に降った雨の量を $2x_i$ とおくと、ダム $i$ に流れ込む雨の量は $x_i + x_{i+1}$ です(ただし、$x_{N+1} = x_1$)。
したがって、問いは次の連立方程式を条件 $x_1 \geq 0, x_2 \geq 0, \cdots, x_N \geq 0
$ のもとで解くことに置き換えられます。

\begin{align}
x_1 + x_2 &= A_1, \\
x_2 + x_3 &= A_2, \\
\vdots \\
x_N + x_1 &= A_N. \\
\end{align}

まず、

A_1 + A_2 + \cdots + A_N = 2(x_1 + x_2 + \cdots + x_N)

ですから、雨量の総和は簡単に求められます。次に、$N$ は奇数である という重要な制約により、

\begin{align}
  & x_1 + x_2 + x_3 + x_4 + \cdots + x_{N-2} + x_{N-1} + x_N \\
= & (x_1 + x_2) + (x_3 + x_4) + \cdots + (x_{N-2} + x_{N-1}) + x_N \\
= & A_1 + A_3 + \cdots + A_{N-2} + x_N
\end{align}

となるので、先ほど求めた雨量の総和から $x_N$ を求めることができます。
$x_N$ がわかったので、連立方程式の第 $N$ 式から $x_1$ も求められます。以下、芋づる式に第 $i$ 式から $x_{i+1}$ を求めることができて、問題が解けました。

答えを出力する際は、山 $i$ の雨量が $2x_i$ であることに注意してください。

感想

これをひらめいたときに「$N$ は奇数である」っていう制約を文中に見つけてガッツポーズしました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?