CF875B.Sorting the Coins

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins together. Their favorite occupation is to sort collections of coins. Sasha likes having things in order, that is why he wants his coins to be arranged in a row in such a way that firstly come coins out of circulation, and then come coins still in circulation.

For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:

  1. He looks through all the coins from left to right;
  2. If he sees that the i-th coin is still in circulation, and (i + 1)-th coin is already out of circulation, he exchanges these two coins and continues watching coins from (i + 1)-th.

Dima repeats the procedure above until it happens that no two coins were exchanged during this procedure. Dima calls hardness of ordering the number of steps required for him according to the algorithm above to sort the sequence, e.g. the number of times he looks through the coins from the very beginning. For example, for the ordered sequence hardness of ordering equals one.

Today Sasha invited Dima and proposed him a game. First he puts n coins in a row, all of them are out of circulation. Then Sasha chooses one of the coins out of circulation and replaces it with a coin in circulation for n times. During this process Sasha constantly asks Dima what is the hardness of ordering of the sequence.

The task is more complicated because Dima should not touch the coins and he should determine hardness of ordering in his mind. Help Dima with this task.

最近,迪马在集邮商店遇见了萨沙,从此他们便一起收集硬币。他们最喜爱的活动是整理硬币收藏。萨沙喜欢井然有序,因此他希望自己的硬币排成一行时,先排列所有已退出流通的硬币,再排列所有仍在流通的硬币。

迪马使用如下算法来整理硬币。该算法的每一步操作如下:

  1. 他从左到右依次检查所有硬币;
  2. 若他发现第 ii 枚硬币仍在流通,而第 (i+1)(i+1) 枚硬币已退出流通,则他交换这两枚硬币,并继续从第 (i+1)(i+1) 枚硬币开始检查。

迪马不断重复上述过程,直到某次完整遍历过程中未发生任何交换为止。迪马将“排序难度”(hardness of ordering)定义为:按上述算法完成排序所需的步骤数,即他从头开始完整遍历硬币序列的次数。例如,对于一个已经排好序的序列,其排序难度为 11。

今天,萨沙邀请迪马并提出一个游戏:他首先将 nn 枚硬币排成一行,全部为已退出流通的硬币;然后,萨沙进行 nn 次操作,每次从中选择一枚已退出流通的硬币,将其替换为一枚仍在流通的硬币。在此过程中,萨沙会不断询问迪马:当前序列的排序难度是多少?

这项任务更具挑战性,因为迪马不能触碰硬币,必须仅凭心算确定排序难度。请帮助迪马完成这项任务。

输入格式

The first line contains single integer n (1 ≤ n ≤ 300 000) — number of coins that Sasha puts behind Dima.

Second line contains n distinct integers _p_1, _p_2, ..., p__n (1 ≤ p__i ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position _p_1, then coin located at position _p_2 and so on. Coins are numbered from left to right.

第一行包含一个整数 nn(1≤n≤300 0001 \leq n \leq 300\,000)—— Sasha 放在 Dima 身后的硬币数量。

第二行包含 nn 个互不相同的整数 p1, p2, …, pnp_1,\,p_2,\,\dots,\,p_n(1≤pi≤n1 \leq p_i \leq n)—— Sasha 将硬币投入循环的位置。Sasha 首先替换位于位置 p1p_1 的硬币,然后替换位于位置 p2p_2 的硬币,依此类推。硬币从左到右编号。

输出格式

Print n + 1 numbers _a_0, _a_1, ..., a__n, where _a_0 is a hardness of ordering at the beginning, _a_1 is a hardness of ordering after the first replacement and so on.

输出 n+1n+1 个数 a0, a1, …, ana_0,\,a_1,\,\dots,\,a_n,其中 a0a_0 表示初始排序的难度,a1a_1 表示第一次替换后的排序难度,依此类推。

输入输出样例

  • 输入#1

    4
    1 3 4 2

    输出#1

    1 2 3 2 1
  • 输入#2

    8
    6 8 3 4 7 2 1 5

    输出#2

    1 2 2 3 4 3 4 5 1

说明/提示

Let's denote as O coin out of circulation, and as X — coin is circulation.

At the first sample, initially in row there are coins that are not in circulation, so Dima will look through them from left to right and won't make any exchanges.

After replacement of the first coin with a coin in circulation, Dima will exchange this coin with next three times and after that he will finally look through the coins and finish the process.

XOOO  →  OOOX

After replacement of the third coin, Dima's actions look this way:

XOXO  →  OXOX  →  OOXX

After replacement of the fourth coin, Dima's actions look this way:

XOXX  →  OXXX

Finally, after replacement of the second coin, row becomes consisting of coins that are in circulation and Dima will look through coins from left to right without any exchanges.

我们用 O 表示未流通的硬币,用 X 表示已流通的硬币。

在第一个样例中,初始时一行中所有硬币均未流通,因此 Dima 将从左到右依次查看这些硬币,且不进行任何交换。

将第一枚硬币替换为已流通硬币后,Dima 会将该硬币向右交换三次,之后他将再次从左到右查看所有硬币并结束整个过程:

XOOO  →  OOOX

将第三枚硬币替换为已流通硬币后,Dima 的操作过程如下:

XOXO  →  OXOX  →  OOXX

将第四枚硬币替换为已流通硬币后,Dima 的操作过程如下:

XOXX  →  OXXX

最后,当第二枚硬币被替换为已流通硬币后,整行硬币均变为已流通状态,Dima 将从左到右依次查看所有硬币,且不进行任何交换。

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

首页