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. Struts2(七)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.xml验证 Struts2提供了验证器,实现了通用的验证逻辑.例如: 非空验证器.长度验证器.日期验证器.email验证器等.具体定 ...

  2. laravel连接多个不同数据库的单例类

    在連接多個不同數據庫時,需要寫多個連接,爲了簡化該操作,可以使用該基類,不同的數據庫只要建立好相對應的類繼承該類,就可以使用ORM模型進行操作了. class singletonInstance { ...

  3. 使用java程序模拟页面发送http的post请求

    在web应用程序中,一般都是通过页面发送http的post请求,但也可以使用java程序来模拟页面发送请求,代码如下: import java.io.BufferedReader; import ja ...

  4. Moya/RxSwift/ObjectMapper/Alamofire开发

    废话不多说直接上代码 // // MoyaNetWorking.swift // GreenAir // // Created by BruceAlbert on 2017/9/18. // Copy ...

  5. CGLib动态代理引起的空指针异常

    一个同事将公司的开发框架基于最新的Spring.Tomcat.Java版本作了部分修改,拿来开发运行之后,发现一个奇怪的空指针异常. 还原一下当时的场景,代码大概如下,所有的Servlet继承自Bas ...

  6. 创建Django工程-Day19

    1. 新建一个day19的工程和app01. 2. 新建templates和static的文件夹. 3. 去settings.py中去做配置. 1)注释掉csrf 2)配置模板路径 'DIRS': [ ...

  7. 【CF600E】Lomsat gelral(dsu on tree)

    [CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...

  8. 【CodeChef】ForbiddenSum

    Portal --> CC ForbiddenSum Solution 场上想到了\(O(NM)\)的做法..然而并没有什么用 首先考虑比较简单的一个问题,给定一个数组\(A\),问这些数不能凑 ...

  9. 【hdu6051】If the starlight never fade

    Portal --> hdu6051 Solution ​ 神仙题qwq好吧我个人感觉是神仙题 ​​ 这题其实有一个比较野路子的做法..就是..打表观察..反正场上ckw大佬就是这样把这题A穿的 ...

  10. python基础----面向对象的程序设计(五个阶段、对小白的忠告、关于OOP常用术语)、类、对象

    一.面向对象的软件开发有如下几个阶段                                              1.面向对象分析(object oriented analysis ,O ...