首页 > 其他分享 >Codeforces875B-Sorting the Coins

Codeforces875B-Sorting the Coins

时间:2022-11-22 19:31:14浏览次数:49  
标签:Codeforces875B circulation 硬币 coins Sasha Coins Sorting Dima coin


Sorting the Coins

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

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

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

Output

Print n + 1 numbers a0, a1, ..., an, where a0 is a hardness of ordering at the beginning, a1

Example

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

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.

 

题意:有n个位置,有n次操作,每次操作在第p[i]位上放上一枚硬币,然后每次放上一枚硬币以后,你要从左往右扫一遍,如果一个硬币右边没有硬币,你就要把他移动到右边有硬币位置,然后在去看下一个硬币,一直重复,知道当前的所有硬币都移到最右边,问你每次操作,需要多少步才能移到最后,初始状态算一步。

题解:放新的一个硬币进来,如果没有放在最右边,那么你的当前操作数应该是上一次的操作数+1,所以最右边有一个硬币,当前操作数就-1

Code:

var n,i,tail,s:longint;p,q,ans:array[0..1000000]of longint;
begin
readln(n);
for i:=1 to n do read(p[i]);
ans[1]:=1;tail:=n;
for i:=1 to n do
begin
q[p[i]]:=1;inc(s);
while(tail>0)and(q[tail]=1)do begin dec(tail);dec(s);end;
ans[i+1]:=s+1;
end;
for i:=1 to n do write(ans[i],' ');
writeln(ans[n+1]);
end.

标签:Codeforces875B,circulation,硬币,coins,Sasha,Coins,Sorting,Dima,coin
From: https://blog.51cto.com/u_15888102/5878391

相关文章

  • [图论记录]arc124D Yet Another Sorting Problem
    题意:给定长度为\(n+m\)的排列\(p\),其中\(1-n\)位置为白色,\(n+1-n+m\)位置为黑色,每次交换一个白色位置与一个黑色位置的数,求把\(p\)变成升序的最少操作次数。link......
  • 【BOI2007】Ranklist Sorting
    【BOI2007】RanklistSortingDescription有一个长为\(n\)的排列\(p\)每次可以选两个位置\(i,j\),然后将\(p_i\)放到\(j\)的位置上,代价为\(i+j\)求将排列变为降序的最小......
  • AtCoder Beginner Contest 277 F Sorting a Matrix
    SortingaMatrix拓扑序首先可以明确无论怎么交换行列,原本在同一行或者同一列的元素,还是会处于同一行或者同一列条件一每行每行地看,如果能满足从小到大的条件,说明第......
  • Solution-P7650 [BalticOI 2007 Day 1] Ranklist Sorting(DP)
    容易发现一条性质:每个人最多只会被移动一次。说明人只有两种:移动的和不移动的。考虑枚举所有不移动的人,并最优化其它人的移动顺序。最开始第\(i\)个人的起点为\(i\),终......
  • 「题解」Codeforces 930E Coins Exhibition
    看到题就先容斥。然后容斥系数太难算了就寄了,大概要分好几种情况讨论,于是就弃了。不容斥也能做。考虑限制将串划分成了若干段,然后一段一段dp.有没有什么好的方法描述这个......
  • asp GridView AllowSorting 不生效
    最近在维护一个N年老项目,想着在Gridview设置 AllowSorting="true" 但排序还不管用。 <asp:GridViewID="GridView1"runat="server"AllowPaging="True"Auto......
  • HDU 2844 Coins
    题目链接:​​传送门​​​题面:ProblemDescriptionWhuacmersusecoins.TheyhavecoinsofvalueA1,A2,A3…AnSilverlanddollar.OnedayHibixopenedpurseandfoun......
  • Universal Atomic Swaps: Secure Exchange of Coins Across All Blockchains
    UniversalAtomicSwaps:SecureExchangeofCoinsAcrossAllBlockchainsThyagarajanSAK,MalavoltaG,Moreno-SánchezP.Universalatomicswaps:Secureexc......
  • ClickHouse 使用Primary Key原因以及为什么与 Sorting Key 不同
    官方地址首先选择主键原因SelectingthePrimaryKey​Thenumberofcolumnsintheprimarykeyisnotexplicitlylimited.Dependingonthedatastructure,you......
  • Sorting Color Balls
    ProblemStatementThereare$N$ballsarrangedfromlefttoright.Thecolorofthe$i$-thballfromtheleftisColor$C_i$,andaninteger$X_i$iswritteno......