0%

Codeforces Round 441(Div. 2) - D. Sorting the Coins

Description

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:

He looks through all the coins from left to right; 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.

Input

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 p1, p2, ..., pn (1 ≤ pi ≤ n) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position p1, then coin located at position p2 and so on. Coins are numbered from left to right.

Output

Print n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the beginning, a1 is a hardness of ordering after the first replacement and so on.

Examples

1
2
3
4
5
6
7
8
9
10
input
4
1 3 4 2
output
1 2 3 2 1
input
8
6 8 3 4 7 2 1 5
output
1 2 2 3 4 3 4 5 1

Note

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.

Key

读题读了半天才懂。n个硬币O面朝上排成一个序列。游戏一共n轮,在每一轮的一开始,所有硬币O面朝上,然后把第p[1]~p[i]个硬币(共i个硬币)翻转为X面。然后进行如下操作: 从左往右遍历整个序列,如果第j个硬币为X且第j+1个硬币为O,交换两者,然后从j+1个硬币继续进行。 持续进行上述遍历,直至最后一遍遍历没有交换任何硬币。把第i轮进行的遍历次数记为a[i]。要求求出每一轮游戏的a[i](以及初始状态下的遍历次数a[0])。

规律是,找出序列最右的O的下标idx(则其右侧为一个连续的X序列或为空),每一轮维护之。计算idx左边有多少X即可。实际计算时,只需判断当前轮翻转的硬币是否使得idx向左移动,若是,a[i]=a[i-1]+1-(new_idx-idx)。否则,a[i]=a[i-1]+1。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
const int maxn = 300000 + 17;
int n, p;
bool s[maxn];
int main() {
ios::sync_with_stdio(false);
cin >> n;
int idx = n + 1;
int res = 1;
cout << "1";
for (int i = 1; i <= n; ++i) {
cin >> p;
s[p] = true;
++res;
if (idx == p + 1) {
int pre_idx = idx;
while (s[--pre_idx]);
++pre_idx;
res -= idx - pre_idx;
idx = pre_idx;
}
cout << ' ' << res;
}
return 0;
}