No.2157 Cliff
タグ : / 解いたユーザー数 111
作問者 :
keisuke6
/ テスター :
kotamanegi
KowerKoint2010
hibit_at
Problem Statement
In "Asakatsu", a virtual contest held every morning, $N$ problems are each set from a predetermined difficulty range.
The difference in difficulty between adjacent problems is called a cliff, and large cliffs are generally not well liked.
Therefore, when selecting problems, it is desirable to make the cliffs small.
The following task is a generalization of this.
Each of the $N$ difficulty ranges $D_i$ contains $M$ problems, given as $D_{i,j}$.
Using these, define each element of a problem set $A$ as follows.
- $A_i(1\leq{i}\leq{N})$ is chosen as one of the problems in difficulty range $D_i$.
However, $A$ must be a non-decreasing sequence.
That is, $A_i \leq A_{i+1}$ must hold for every $1 \leq i \leq N-1$.
Here, define $f(A)$ for $A$ as "the maximum value of $A_{i+1}-A_i$ over all $i$". Find the minimum value of $f(A)$.
If no $A$ satisfying the condition exists, output -1.
Input
$N\ M$
$D_{1,1} , D_{1,2} , \cdots , D_{1,M}$
$D_{2,1} , D_{2,2} , \cdots , D_{2,M}$
$\hspace{8pt}\vdots \hspace{19pt} \vdots \hspace{9pt}\ddots \hspace{11pt}\vdots$
$D_{N,1} , D_{N,2} , \cdots , D_{N,M}$
- $1\leq{N,M}\leq{1500}$
- $1\leq{D_{i,j}}\leq{10^9}$
Output
Output the minimum value of $f(A)$ on one line. If no $A$ satisfying the condition exists, output -1.
Sample
Sample 1
Input
2 2 5 8 12 7
Output
2
$A=(5,7)$ is optimal.
Sample 2
Input
4 4 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7
Output
0
$A=(4,4,4,4)$ is optimal.
Sample 3
Input
3 2 2 4 1 5 3 2
Output
-1
If no $A$ satisfying the condition exists, output -1.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。