AAAA 1 P3 - Topographical Sort
View as PDFAfter analyzing the triple peaks of the Cordillera Oriental, expert topographers (including yourself) have shifted their focus onto the rising sea levels, and their effects on the DMOJistan islands.
The DMOJistan landscape is described as an array of integers,
, where
denotes the elevation of the
patch of land from the left border of DMOJistan. It is known that all elevations
are in the inclusive range
, and that the patches of land just outside DMOJistan have zero elevation, i.e.
.
For a given sea level , a contiguous segment of land from index
to
is called an island if all patches of land within it have elevations greater than or equal to sea level, and the patches of land to its immediate left and right are both below sea level. Formally, for integers
and
satisfying
, the segment of land from index
to
is an island if and only if
and
.
Let denote the number of distinct islands there would be in DMOJistan if the sea level was
.
You will be given an integer , indicating which of the following subtasks you must solve:
Subtask 1:
The year is 2025, and DMOJistan has not yet been submerged in water. After measuring DMOJistan's elevation data, , you would like to predict how the number of islands will change as sea levels rise. Compute the values of
.
Subtask 2:
The year is 3000, and sea levels have risen so high that DMOJistan has become completely submerged. The only remaining info on DMOJistan's landscape are the values of , recorded by a topographer of the past. You must now make an educated guess on what the topography of DMOJistan was long ago based on this data.
Determine any landscape described by integer elevations in the range
,
, which would have produced the given values of
as sea levels rose. If such a landscape cannot exist, output
-1 instead.
Constraints
Subtask 1 [30%]
Subtask 2 [70%]
Input Specification
The first line contains one integer, , the number of the subtask you must solve.
The second line contains one integer, .
The third line contains space-separated integers, describing
if
, or
if
.
Output Specification
If , output one line containing
space-separated integers,
.
If , output
-1 on one line if a landscape under the given constraints does not exist. Otherwise, output space-separated integers in the range
,
, describing any landscape which would have produced the given values of
as sea levels rose.
Sample Input 1
1
6
2 1 3 0 4 3
Sample Output 1
2 3 2 1 0 0
Explanation For Sample 1
Since , we are given
and would like to compute
. The number of islands at each sea level
from
to
can be counted in each of the following diagrams.


Sample Input 2
2
5
1 3 3 2 1
Sample Output 2
3 1 4 1 5
Explanation for Sample 2
Since , we are given
and would like to find any landscape
which matches the data. The sample output is one possible landscape which satisfies these constraints. This can be verified in the following diagrams for each sea level
from
to
:


Sample Input 3
2
5
1 2 3 4 5
Sample Output 3
-1
Explanation for Sample 3
It can be shown that there does not exist a landscape with patches of land and elevations
in the inclusive range
which produces the given values of
.
Comments