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.
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.
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.
4 1 3 4 2
1 2 3 2 1
8 6 8 3 4 7 2 1 5
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.
//能看懂题是多么的关键
题意:首先硬币都是不能流通的,给你摆放硬币的顺序,然后让你得出硬币都移到最后(最后是连续流通的硬币)的顺序
例 4
1 3 4 2
首先是流通的硬币放在第一位
xooo--->ooox,只用1步,因为当你再次需要从头从左往右看的时候你才是进入下一次的循环.
然后xoxo--->oxox--->ooxx需要2步,
然后xoxx--->oxxx,只用1步
然后xxxx, 用0步,,,,因为题上说起始步为1,所以都再加上1,
得到1 (起始)2321,
题解
需要的步数=放硬币的数量-最右边连续的硬币数+1
我们可以看做从需要移动的最右边的硬币开始往右移动到最右边,直到最左边的硬币移动到最右边,那么每个硬币往右移动一次,所以总移动次数就是除了最右边硬币,其他硬币个数的总和
#include<cstdio>
#include<algorithm>
using namespace std;
int b[300001];//表示是否在最后的连续排列上,,,最后连续硬币的话表示为1
int main()
{
int x,t,k,i,n;
scanf("%d",&t);
printf("1 ");
n=t;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
b[x]=1;
while(b[n])
n--;
printf(" %d",i+n-k+1);//用总共现在 放进的流通硬币减去最后的连续的硬币数 +1
}
return 0;
}