Codeforces 875B (Round 441 D1) Sorting the Coins

http://codeforces.com/problemset/problem/875/B

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

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  →  OXXXRound #

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个位置放硬币,第i次放到从a[1]到第a[i]的位置,通过移动,把所有的硬币放到最右边,问一共移动多少次;
规则:从左往右看,如果硬币的右边没有硬币,则可以移动,一次可移动多位直到有硬币的地方。
输出第i个次放硬币需要移动的次数。

样例一:
一、1000-》0001 一次
二、1010-》0101-》0011 二次
三、1011-》0111一次
四、1111-》0次
因为初始次数是1,所以所有次数都加上1
先输出初始的次数(难度):
所以输出1 1 2 3 2 1

题解

ans=放硬币的数量-最右边连续的硬币数+1
我们可以看做从需要移动的最右边的硬币开始往右移动到最右边,直到最左边的硬币移动到最右边,那么每个硬币往右移动一次,所以总移动次数就是除了最右边硬币,其他硬币个数的总和,比如:
样例一:
二、1010->1001->0011

CODE:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int a,vis[300001];
int main()
{
    int n;
    scanf("%d",&n);
    cout << '1';
    int k=n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        vis[a]=1;
        while(vis[k])
            k--;
        printf(" %d",i+k-n+1);
    }
    cout <<endl;
    return 0;
}
Codeforces Round 887是一个程序设计竞赛活动,由Codeforces组织举办。根据引用中的代码,该竞赛的题目要求解决一个序列操作的问题。给定一个长度为n的序列,通过执行一系列操作,使得序列变得非sorted,即非严格递增。具体操作是将序列中[1, i]范围内的数字全部加一,同时将[i+1, n]范围内的数字全部减一。问题要求求解最少需要执行多少次操作才能达到要求。 引用中的代码给出了解决这个问题的实现。代码首先读入序列的长度n和序列a。然后通过判断序列是否已经是非sorted,如果是则直接输出0。接下来,代码遍历序列,求出相邻两个数字的差的最小值。最后,计算出最少需要执行的操作次数,并输出结果。 需要注意的是,引用中的代码只是给出了解决问题的一种实现方式,并不代表Codeforces Round 887的具体题目和解答。要了解该竞赛的具体信息,需要参考Codeforces官方网站或相关资料。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Codeforces Round 887 (Div. 2)](https://blog.csdn.net/qq_36545889/article/details/131905067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值