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的序列,开始都是O,每次把一个位置改为X

问冒泡排序扫多少遍可以,可以确定把所有的X换到了后面

(每遇到XO就交换)

相当于 找到序列的最后一个O,统计它前面有多少个X

#include<cstdio>

using namespace std;

bool a[];

int main()
{
int n;
scanf("%d",&n); printf("");
int ans=,pos=n,x;
while(n--)
{
scanf("%d",&x);
++ans; a[x]=true;
while(a[pos]) pos--,ans--;
printf(" %d",ans);
}
}

codeforces 876 D. Sorting the Coins的更多相关文章

  1. codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))

    题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...

  2. Codeforces D. Sorting the Coins

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

  3. Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...

  4. Codeforces Round #441 D. Sorting the Coins(模拟)

    http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...

  5. CodeForces - 876D Sorting the Coins

    题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换.序 ...

  6. codeforces 876 C. Classroom Watch

    http://codeforces.com/contest/876/problem/C C. Classroom Watch time limit per test 1 second memory l ...

  7. codeforces 876 F. High Cry(思维)

    题目链接:http://codeforces.com/contest/876/problem/F 题解:一道简单的思维题,知道最多一共有n*(n+1)/2种组合,不用直接找答案直接用总的组合数减去不符 ...

  8. Codeforces Round #335 Sorting Railway Cars 动态规划

    题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...

  9. Codeforces 911E - Stack Sorting

    911E - Stack Sorting 思路: 用栈来模拟,能pop就pop,记下一个需要pop的数为temp,那么如果栈非空,栈顶肯定大于temp,那么加入栈 栈顶值-1 到 temp 的值,否则 ...

随机推荐

  1. 2018软工实践—Alpha冲刺(5)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助测试的进行 测试项目运行的服务器环境 ...

  2. HDU 5672 String

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  3. 多tab点击切换

    现在来一个小练习,就是用js实现多tab之间的切换: <body> <ul id="tab"> <li id="tab1"> ...

  4. 1st 构建之法读后感

    构建之法读后感 由于时间和书的篇幅所限,所以我没能真正通读全书,只通过网上的介绍和书内前言及目录,大概了解了构建之法是一本怎样的一本书. 这本书是由具有长达20年一线软件开发经验的邹欣老师所撰写,他以 ...

  5. HUST1017-Exact Cover

    给出一个\(n\times m\)的01矩阵,每行最多有\(c\)个1,求一个精确覆盖,即选出一些行使得每列有有且仅有一个1.输出方案. 分析 被这个题坑到了啊!!第一次上HUSTOJ做题,不知道没有 ...

  6. 【uoj#139】[UER #4]被删除的黑白树 贪心

    题目描述 给出一个 $n$ 个节点的树,$1$ 号点为根.现要将其中一些点染成黑色,使得每个叶子节点(不包括根节点)到根节点路径上的黑点数相同.求最多能够染多少个黑点. 题解 贪心 显然有结论:选择的 ...

  7. 转---秒杀多线程第八篇 经典线程同步 信号量Semaphore

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  8. 转:机器学习 规则化和模型选择(Regularization and model selection)

    规则化和模型选择(Regularization and model selection) 转:http://www.cnblogs.com/jerrylead/archive/2011/03/27/1 ...

  9. 如何在vi中设置tab的长度

    在使用vi写python时(其实,不管用什么写python程序时,都要注意),首先要将Tab键的长度设为4,因为使用python编程时,我们是通过缩进来实现作用域的,所以要统一Tab键的长度.具体方法 ...

  10. Atom实用配置插件for C++

    autocomplete-clang  自动补全 autocomplete for C/C++/ObjC using clang autocomplete-python 自动补全 Python pac ...