#C2015. Harmonious Sequence
Harmonious Sequence
Harmonious Sequence
You are given a performance schedule with S stages, where each stage offers A performance acts. Each act has an entertainment value. Your task is to determine whether it is possible to select exactly one act from each stage such that for every two consecutive stages, the absolute difference of their chosen entertainment values does not exceed a given limit D.
Formally, let the entertainment value chosen at stage i be v_i. The selection is valid if and only if for every i from 1 to S-1,
\( |v_i - v_{i+1}| \le D \)
If such a selection exists, output Yes
; otherwise, output No
.
inputFormat
The first line of input contains three integers S, A and D, where S is the number of stages, A is the number of acts per stage, and D is the maximum allowed absolute difference between the entertainment values of consecutive acts.
This is followed by S lines, each containing A integers. The j-th integer in the i-th line represents the entertainment value of the j-th act in the i-th stage.
outputFormat
Output a single line containing either Yes
if it is possible to choose one act per stage to meet the condition, or No
otherwise.
3 3 4
1 5 9
2 6 10
3 7 11
Yes