CF798E.Mike and code of a permutation

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Mike has discovered a new way to encode permutations. If he has a permutation P = [_p_1, _p_2, ..., p__n], he will encode it in the following way:

Denote by A = [_a_1, _a_2, ..., a__n] a sequence of length n which will represent the code of the permutation. For each i from 1 to n sequentially, he will choose the smallest unmarked j (1 ≤ j ≤ n) such that p__i < p__j and will assign to a__i the number j (in other words he performs a__i = j) and will mark j. If there is no such j, he'll assign to a__i the number  - 1 (he performs a__i =  - 1).

Mike forgot his original permutation but he remembers its code. Your task is simple: find any permutation such that its code is the same as the code of Mike's original permutation.

You may assume that there will always be at least one valid permutation.

迈克发现了一种对排列进行编码的新方法。若他有一个排列 P=[p1,p2,…,pn]P = [p_1, p_2, \dots, p_n],则其编码方式如下:

记 A=[a1,a2,…,an]A = [a_1, a_2, \dots, a_n] 为一个长度为 nn 的序列,它将表示该排列的编码。对于每个从 11 到 nn 的 ii,他依次执行以下操作:在所有未被标记的 jj(其中 1≤j≤n1 \le j \le n)中,选出满足 pi<pjp_i < p_j 的最小 jj,并将该 jj 赋值给 aia_i(即令 ai=ja_i = j),然后将 jj 标记为已使用;若不存在这样的 jj,则令 ai=−1a_i = -1。

迈克忘记了原始排列,但他还记得其编码。你的任务很简单:找出任意一个排列,使其编码与迈克原始排列的编码完全相同。

你可以假定总存在至少一个合法的排列。

输入格式

The first line contains single integer n (1 ≤ n ≤ 500 000) — length of permutation.

The second line contains n space-separated integers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ n or a__i =  - 1) — the code of Mike's permutation.

You may assume that all positive values from A are different.

第一行包含一个整数 nn(1≤n≤500 0001 \leq n \leq 500\,000)—— 排列的长度。

第二行包含 nn 个以空格分隔的整数 a1, a2, …, ana_1,\,a_2,\,\dots,\,a_n(1≤ai≤n1 \leq a_i \leq n 或 ai=−1a_i = -1)—— Mike 的排列的编码。

你可以假设 AA 中所有正数值互不相同。

输出格式

In first and only line print n numbers _p_1, _p_2, ..., p__n (1 ≤ p__i ≤ n) — a permutation P which has the same code as the given one. Note that numbers in permutation are distinct.

在第一行且仅在第一行输出 nn 个数 p1, p2, …, pnp_1,\ p_2,\ \dots,\ p_n(其中 1≤pi≤n1 \leq p_i \leq n),即一个与给定编码相同的排列 PP。注意:排列中的数字互不相同。

输入输出样例

  • 输入#1

    6
    2 -1 1 5 -1 4

    输出#1

    2 6 1 4 5 3
  • 输入#2

    8
    2 -1 4 -1 6 -1 8 -1

    输出#2

    1 8 2 7 3 6 4 5

说明/提示

For the permutation from the first example:

i = 1, the smallest j is 2 because _p_2 = 6 > _p_1 = 2.

i = 2, there is no j because _p_2 = 6 is the greatest element in the permutation.

i = 3, the smallest j is 1 because _p_1 = 2 > _p_3 = 1.

i = 4, the smallest j is 5 (2 was already marked) because _p_5 = 5 > _p_4 = 4.

i = 5, there is no j because 2 is already marked.

i = 6, the smallest j is 4 because _p_4 = 4 > _p_6 = 3.

对于第一个样例中的排列:

i = 1 时,最小的 j 是 2,因为 _p_₂ = 6 > _p_₁ = 2。

i = 2 时,不存在满足条件的 j,因为 _p_₂ = 6 是该排列中最大的元素。

i = 3 时,最小的 j 是 1,因为 _p_₁ = 2 > _p_₃ = 1。

i = 4 时,最小的 j 是 5(2 已被标记),因为 _p_₅ = 5 > _p_₄ = 4。

i = 5 时,不存在满足条件的 j,因为 2 已被标记。

i = 6 时,最小的 j 是 4,因为 _p_₄ = 4 > _p_₆ = 3。

输入解题思路,AI测评打分。不知道怎么写?

首页