Help in a Data Structure Problem

Правка en3, от goku111, 2020-04-16 10:10:30

Recently I was trying out a data structure problem and I couldn't solve it. Can anybody please help me to solve the problem, I would also like to know how to approach such problems.

Problem: You had lost a permutation p={1..N} and want to retrieve it. For each index 1<=i<=N you have the following information

1) The index j of the first number to the left of i such that p[j]>p[i].(It will be 0 if there is no such index)

2) the index j of the first number to the right of i such that p[j]>p[i].(It will be 0 if there is no such index)

The constraint on N is 5*10^5.

If multiple solution exists print the lexicographically smallest permutation.

Sample Input:

5

0 2

0 0

2 0

3 5

3 0

Sample Output: 1 5 4 2 3

Note: 4 5 3 1 2 is also valid but not the lexicographically smallest permutation.

Теги #data structure, #help, #algorithms

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский goku111 2020-04-16 10:10:30 23 Tiny change: '3\n\nNote:4 5 3 1 2 ' -> '3\n\nNote: 4 5 3 1 2 '
en2 Английский goku111 2020-04-16 10:08:30 156
en1 Английский goku111 2020-04-16 09:21:03 692 Initial revision (published)