102603: [AtCoder]ABC260 D - Draw Your Cards

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $400$ points

Problem Statement

There is a deck consisting of $N$ face-down cards with an integer from $1$ through $N$ written on them. The integer on the $i$-th card from the top is $P_i$.

Using this deck, you will perform $N$ moves, each consisting of the following steps:

  • Draw the topmost card from the deck. Let $X$ be the integer written on it.
  • Stack the drawn card, face up, onto the card with the smallest integer among the face-up topmost cards on the table with an integer greater than or equal to $X$ written on them. If there is no such card on the table, put the drawn card on the table, face up, without stacking it onto any card.
  • Then, if there is a pile consisting of $K$ face-up cards on the table, eat all those cards. The eaten cards all disappear from the table.

For each card, find which of the $N$ moves eats it. If the card is not eaten until the end, report that fact.

Constraints

  • All values in input are integers.
  • $1 \le K \le N \le 2 \times 10^5$
  • $P$ is a permutation of $(1,2,\dots,N)$ (i.e. a sequence obtained by rearranging $(1,2,\dots,N)$).

Input

Input is given from Standard Input in the following format:

$N$ $K$
$P_1$ $P_2$ $\dots$ $P_N$

Output

Print $N$ lines.
The $i$-th line ($1 \le i \le N$) should describe the card with the integer $i$ written on it. Specifically,

  • if the card with $i$ written on it is eaten in the $x$-th move, print $x$;
  • if that card is not eaten in any move, print $-1$.

Sample Input 1

5 2
3 5 2 1 4

Sample Output 1

4
3
3
-1
4

In this input, $P=(3,5,2,1,4)$ and $K=2$.

  • In the $1$-st move, the card with $3$ written on it is put on the table, face up, without stacked onto any card.
  • In the $2$-nd move, the card with $5$ written on it is put on the table, face up, without stacked onto any card.
  • In the $3$-rd move, the card with $2$ written on it is stacked, face up, onto the card with $3$ written on it.
    • Now there is a pile consisting of $K=2$ face-up cards, on which $2$ and $3$ from the top are written, so these cards are eaten.
  • In the $4$-th move, the card with $1$ written on it is stacked, face up, onto the card with $5$ written on it.
    • Now there is a pile consisting of $K=2$ face-up cards, on which $1$ and $5$ from the top are written, so these cards are eaten.
  • In the $5$-th move, the card with $4$ written on it is put on the table, face up, without stacked onto any card.
  • The card with $4$ written on it was not eaten until the end.

Sample Input 2

5 1
1 2 3 4 5

Sample Output 2

1
2
3
4
5

If $K=1$, every card is eaten immediately after put on the table within a single move.


Sample Input 3

15 3
3 14 15 9 2 6 5 13 1 7 10 11 8 12 4

Sample Output 3

9
9
9
15
15
6
-1
-1
6
-1
-1
-1
-1
6
15

Input

题意翻译

依次摸 $n$ 张卡片,第 $i$ 个卡片上写的 $p_i$,$p_i$ 是 $1$ 到 $n$ 的一个排列。 维护一些牌堆,如果 $p_i$ 大于所有堆顶的牌,那么新开一堆只有 $p_i$。 否则在所有堆顶的牌,找到大于 $p_i$ 最小的一张,把 $p_i$ 放到这个堆的堆顶。 如果这一堆有恰好 $K$ 张牌,把这 $K$ 个牌的数字都标记上当前的时间 $i$,并把这堆删掉。 最后输出每个数字被标记的时间,如果没有被标记过就是 $-1$。 Translated by @[$\tt{\_YXJS\_}$](/user/516346).

Output

得分:400分

问题描述

有一个由$N$张牌组成的牌堆,每张牌的正面都写有一个从1到$N$的整数。在牌堆顶部的第$i$张牌上写的数字是$P_i$。

使用这个牌堆,你将进行$N$次操作,每次操作包含以下步骤:

  • 从牌堆顶部抽取一张牌。设该牌上写的数字为$X$。
  • 将抽取的牌正面朝上放在桌子上一张牌的顶部,这张牌需要满足以下条件:它是最顶部的一张牌,且牌面上的数字大于等于$X$,并且在所有满足条件的牌中,这张牌的数字最小。如果没有满足条件的牌在桌子上,将抽取的牌放在桌子上,正面朝上,不放在任何牌的顶部。
  • 然后,如果桌子上有一叠由$K$张牌组成的牌堆,将这叠牌吃掉。被吃掉的牌都会从桌子上消失。

对于每张牌,找出它在$N$次操作中哪一次被吃掉。如果它在任何一次操作中都没有被吃掉,报告这一事实。

约束

  • 输入中的所有值都是整数。
  • $1 \le K \le N \le 2 \times 10^5$
  • $P$是序列$(1,2,\dots,N)$的排列(即通过重新排列$(1,2,\dots,N)$得到的序列)。

输入

从标准输入给出输入,格式如下:

$N$ $K$
$P_1$ $P_2$ $\dots$ $P_N$

输出

打印$N$行。
第$i$行($1 \le i \le N$)应该描述写有数字$i$的牌。具体来说,

  • 如果写有数字$i$的牌在第$x$次操作中被吃掉,打印$x$;
  • 如果这张牌在任何操作中都没有被吃掉,打印$-1$。

样例输入1

5 2
3 5 2 1 4

样例输出1

4
3
3
-1
4

在这个输入中,$P=(3,5,2,1,4)$,$K=2$。

  • 在第1次操作中,写有数字3的牌被放在桌子上,正面朝上,不放在任何牌的顶部。
  • 在第2次操作中,写有数字5的牌被放在桌子上,正面朝上,不放在任何牌的顶部。
  • 在第3次操作中,写有数字2的牌被放在写有数字3的牌的顶部,正面朝上。现在有一叠由$K=2$张牌组成的牌堆,最上面两张牌的数字是2和3,所以这些牌被吃掉了。
  • 在第4次操作中,写有数字1的牌被放在写有数字5的牌的顶部,正面朝上。现在有一叠由$K=2$张牌组成的牌堆,最上面两张牌的数字是1和5,所以这些牌被吃掉了。
  • 在第5次操作中,写有数字4的牌被放在桌子上,正面朝上,不放在任何牌的顶部。
  • 写有数字4的牌在最后没有被吃掉。

样例输入2

5 1
1 2 3 4 5

样例输出2

1
2
3
4
5

如果$K=1$,那么每张牌在被放在桌子上后,在一次操作中就会立即被吃掉。


样例输入3

15 3
3 14 15 9 2 6 5 13 1 7 10 11 8 12 4

样例输出3

9
9
9
15
15
6
-1
-1
6
-1
-1
-1
-1
6
15

加入题单

算法标签: