304594: CF875B. Sorting the Coins

Memory Limit:512 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Sorting the Coins

题意翻译

## 题意 现有 $n(1\le n\le 3\times 10^5)$ 枚硬币,对其进行如下操作: 1. 从第 $1$ 枚硬币向第 $n$ 枚顺次遍历。 2. 若第 $i$ 枚硬币反面朝上且第 $i+1$ 枚正面朝上,交换这两枚硬币的位置。 重复以上操作直到没有发生交换。 开始时,所有硬币都正面朝上。给出 $n$ 次修改,第 $i$ 次修改是在第 $i-1$ 次修改的初状态(即操作前)的基础上将第 $p_i(\{p_1,p_2,\ldots,p_n\}=\{1,2,\ldots,n\})$ 枚硬币从正面朝上翻转为反面朝上,每次修改(包括第 $0$ 次)后请求出操作次数。

题目描述

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.

输入输出格式

输入格式


The first line contains single integer $ n $ ( $ 1<=n<=300000 $ ) — 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.

输出格式


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.

输入输出样例

输入样例 #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.

Input

题意翻译

## 题意 现有 $n(1\le n\le 3\times 10^5)$ 枚硬币,对其进行如下操作: 1. 从第 $1$ 枚硬币向第 $n$ 枚顺次遍历。 2. 若第 $i$ 枚硬币反面朝上且第 $i+1$ 枚正面朝上,交换这两枚硬币的位置。 重复以上操作直到没有发生交换。 开始时,所有硬币都正面朝上。给出 $n$ 次修改,第 $i$ 次修改是在第 $i-1$ 次修改的初状态(即操作前)的基础上将第 $p_i(\{p_1,p_2,\ldots,p_n\}=\{1,2,\ldots,n\})$ 枚硬币从正面朝上翻转为反面朝上,每次修改(包括第 $0$ 次)后请求出操作次数。

加入题单

算法标签: