博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 876 D. Sorting the Coins
阅读量:6986 次
发布时间:2019-06-27

本文共 3541 字,大约阅读时间需要 11 分钟。

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
using namespace std;bool a[300001];int main(){ int n; scanf("%d",&n); printf("1"); int ans=1,pos=n,x; while(n--) { scanf("%d",&x); ++ans; a[x]=true; while(a[pos]) pos--,ans--; printf(" %d",ans); }}

 

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7711968.html

你可能感兴趣的文章
第十一天:find
查看>>
golang sync WaitGroup
查看>>
使用graphite和grafana进行应用程序监控
查看>>
github推送错误:已经有此代码,不允许覆盖的解决方法
查看>>
C#MysqlHelper
查看>>
SpringMVC Hello World 实例
查看>>
MySQL BETWEEN 用法
查看>>
vim开启自动缩进
查看>>
【转】js之iframe子页面与父页面通信
查看>>
java设计模式_模版模式
查看>>
摄像机平滑更随脚本
查看>>
Struts2 标签配置详细
查看>>
需求管理工具比较 Doors_Requistie Pro_RDM
查看>>
centos+php+nginx的php.ini无法加载的问题
查看>>
从菜鸟到专家的五步编程语言学习法
查看>>
RequestQueue
查看>>
Android TextView 属性设置
查看>>
html元素分类以及嵌套规则
查看>>
android dpi
查看>>
C语言的预处理、编译、汇编、链接
查看>>