No.602 The Hidden Game 2
タグ : / 解いたユーザー数 66
作問者 :
nmnmnmnmnmnmnm
Problem Statement
This year too, when I opened the drawer of my father's desk, I found another game hidden there.
I decided to play it without permission again this time.
This game is played by moving a character around an infinitely large field paved with square cells.
Because the creator has a nasty personality, moving is a little troublesome.
The character can move to any cell whose Manhattan distance from the current cell is one of $d_1 \dots d_n$.
The Manhattan distance between $(x_1,y_1)$ and $(x_2,y_2)$ is expressed as $|x_1-x_2|+|y_1-y_2|$.
The character is now at the cell $(0,0)$.
I wanted to move from $(0,0)$ to $(x,y)$, but it seems my father will be home soon.
Since I really want to get to $(x,y)$, I have decided to make only $2$ more moves.
Find the minimum number of moves needed to get to $(x,y)$.
If it cannot be reached within $2$ moves, output $-1$.
Input
$n$ $d_1 \dots d_n$ $x \ y$
Line $1$ gives $n$, the number of distances that can be moved.
Line $2$ gives $d_i$ separated by spaces.
Line $3$ gives the coordinates $(x,y)$ of the destination cell separated by a space.
All inputs are given as integers.
$1 \le n \le 10^5$
$1 \le d_i \le 10^9 \ (d_i$はすべて異なる$)$
$-10^9 \le x,y \le 10^9$
Output
Output the minimum number of moves to $(x,y)$.
If it cannot be reached within $2$ moves, output $-1$.
Sample
Sample 1
Input
4 3 5 4 13 8 0
Output
2
$(3,0)→(8,0)$, $(4,0)→(8,0)$, $(13,0)→(8,0)$, etc.
Sample 2
Input
1 5 -2 -3
Output
1
Sample 3
Input
1 14 51 4
Output
-1
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。