No.731 Love for Arithmetic Progressions
Problem Statement
Rian, who loves arithmetic progressions, found a sequence with $N$ elements by the roadside.
Because Rian loves arithmetic progressions so much, he decided to ask the Sequence Old Man, who can change the value of each element of a sequence, to turn the sequence he found into an arithmetic progression.
However, the Sequence Old Man charges a cost of $(a_i - b_i)^2$ to change the $i$-th element $a_i$ of the sequence to $b_i$. (Also, he will change the value of each element at most once.)
For example, the cost of changing the sequence {3, 3, 4} to {5, 5, 5} is 4 + 4 + 1 = 9, and the cost of changing it to {3, 3.5, 4} is 0 + 0.25 + 0 = 0.25.
Rian wants to obtain an arithmetic progression at as low a cost as possible, so find the first term $b_1$ and the common difference $d$ that minimize the cost, as well as the cost $c$ incurred in that case.
Input
$N$ $a_1 \ a_2 \ \ldots \ a_N$
The first line contains the number of elements $N$ of the sequence.
The second line contains the values $a_i \ (1 \le i \le N)$ of the elements of the sequence, separated by spaces.
All inputs are integers and satisfy the following constraints.
- $2 \le N \le 1000$
- $0 \le a_i \le 10^5$
Output
On the first line, output the first term $b_1$ and the common difference $d$ of the resulting arithmetic progression that minimizes the cost, separated by a space.
On the second line, output the cost $c$ incurred in that case.
For each value, an error of at most $10^{-6}$ is allowed.
Print a newline at the end.
Sample
Sample 1
Input
3 3 3 4
Output
2.83333333333333 0.5 0.166666666666667
Sample 2
Input
5 0 0 0 0 10000
Output
-2000 2000 40000000
$b_1$ may be negative.
Sample 3
Input
7 6 5 4 3 2 1 0
Output
6 -1 0
$d$ may be negative, and $c$ may be zero.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
りあん
ixmel