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

D. Sorting the Coins
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

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  →  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次使得n个硬币流通,小明每次只能从左到右看,不能返回,且不能用手改变位置,若看的位置是流通硬币且下一个硬币是不流通的,则小明则在心中将这两个硬币交换位置,若前后两个硬币均为流通或不流通则不交换,问每次将此时流通的硬币全部排到最后的难度是多少(因为每次看完要记住当前的位置,所以看的次数越多越难,不用看时的难度为1多看一次加一)。

思路:已经在最后的相连的流通硬币是不会交换的,然后每次将一个流通硬币换到下一个流通硬币时这个流通硬币就不能再换的,要开始换下一个流通硬币,等同于就是每次看把最后的没有在尾部的流通硬币移到尾部,即每次看的次数就是没有连接在尾部的流通硬币个数。难度+1


#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#define LL long long
const LL maxn = 300000+10;
LL n,p[maxn],sum,vis[maxn],last;
int main()
{
    scanf("%lld",&n);
    for(int i=0;i<n;i++) scanf("%lld",&p[i]);
    last = n;sum = 0;
    printf("1");
    for(int i=0;i<n;i++){
        vis[p[i]]=1; sum++;
        while(vis[last]) sum --,last--;
        printf(" %lld",sum+1);
    }
    printf("\n");
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值