#abc337c. C - Lining Up 2

C - Lining Up 2

Score: 300300 points

问题描述

NN 名人员站成一排:第 1 号人员、第 2 号人员、……、第 NN 号人员。

你得到的人员排列顺序是一个长度为 NN 的序列 A=(A1,A2,,AN)A=(A_1,A_2,\ldots,A_N)

其中,Ai (1iN)A_i\ (1\leq i\leq N) 表示如下信息:

  • Ai=1A_i=-1,表示第 ii 号人员站在队伍的最前面;
  • Ai1A_i\neq -1,表示第 ii 号人员紧跟在第 AiA_i 号人员后面。

请按照从前到后的顺序输出队伍中人员的编号。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN people standing in a line: person 11, person 22, \ldots, person NN.

You are given the arrangement of the people as a sequence A=(A1,A2,,AN)A=(A _ 1,A _ 2,\ldots,A _ N) of length NN.

Ai (1iN)A _ i\ (1\leq i\leq N) represents the following information:

  • if Ai=1A _ i=-1, person ii is at the front of the line;
  • if Ai1A _ i\neq -1, person ii is right behind person AiA _ i.

Print the people's numbers in the line from front to back.

Constraints

  • 1N3×1051\leq N\leq3\times10 ^ 5
  • Ai=1A _ i=-1 or 1AiN (1iN)1\leq A _ i\leq N\ (1\leq i\leq N)
  • There is exactly one way to arrange the NN people consistent with the information given.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A _ 1 A2A _ 2 \ldots ANA _ N

Output

If person s1s _ 1, person s2s _ 2, \ldots, person sNs _ N are standing in the line in this order, print s1s _ 1, s2s _ 2, \ldots, and sNs _ N in this order, separated by spaces.

Sample Input 1

6
4 1 -1 5 3 2

Sample Output 1

3 5 4 1 2 6

If person 33, person 55, person 44, person 11, person 22, and person 66 stand in line in this order from front to back, the arrangement matches the given information.

Indeed, it can be seen that:

  • person 11 is standing right behind person 44,
  • person 22 is standing right behind person 11,
  • person 33 is at the front of the line,
  • person 44 is standing right behind person 55,
  • person 55 is standing right behind person 33, and
  • person 66 is standing right behind person 22.

Thus, print 33, 55, 44, 11, 22, and 66 in this order, separated by spaces.

Sample Input 2

10
-1 1 2 3 4 5 6 7 8 9

Sample Output 2

1 2 3 4 5 6 7 8 9 10

Sample Input 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

Sample Output 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14

update @ 2024/3/10 01:27:17