Codeforces875B-Sorting the Coins
来源:互联网 发布:linux vi强制退出命令 编辑:程序博客网 时间:2024/05/21 17:09
Sorting the Coins
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.
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.
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.
41 3 4 2
1 2 3 2 1
86 8 3 4 7 2 1 5
1 2 2 3 4 3 4 5 1
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次操作,每次操作在第p[i]位上放上一枚硬币,然后每次放上一枚硬币以后,你要从左往右扫一遍,如果一个硬币右边没有硬币,你就要把他移动到右边有硬币位置,然后在去看下一个硬币,一直重复,知道当前的所有硬币都移到最右边,问你每次操作,需要多少步才能移到最后,初始状态算一步。
var n,i,tail,s:longint;p,q,ans:array[0..1000000]of longint;begin readln(n); for i:=1 to n do read(p[i]); ans[1]:=1;tail:=n; for i:=1 to n do begin q[p[i]]:=1;inc(s); while(tail>0)and(q[tail]=1)do begin dec(tail);dec(s);end; ans[i+1]:=s+1; end; for i:=1 to n do write(ans[i],' '); writeln(ans[n+1]);end.
- Codeforces875B-Sorting the Coins
- 【Codeforces875B】Sorting the Coins
- CF D. Sorting the Coins
- CodeForces 876D Sorting the Coins
- Codeforces 876 D. Sorting the Coins 模拟
- Codeforces Round #441 D. Sorting the Coins
- Codeforces 875B Sorting the Coins
- codeforces 876D Sorting the Coins
- Codeforces Round #441 D. Sorting the Coins
- Codeforces 875B Sorting the Coins 题解
- [冒泡 反序表] Codeforces #876D. Sorting the Coins
- Codeforces Round #441 D. Sorting the Coins(线段树)
- #440 Div.2 D.Sorting the Coins 硬币排序问题
- codeforces——875B —— Sorting the Coins
- Codeforces 875B (Round 441 D1) Sorting the Coins
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins 乱搞
- 【反序表+树状数组】Codeforces Round #441(Div.2)D[Sorting the Coins]题解
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins
- 基础练习 杨辉三角形
- C++的变量
- C语言再学习 -- 段错误(核心已转储)
- struts2的工作原理
- android中getLocationInWindow和getLocationOnScreen的区别
- Codeforces875B-Sorting the Coins
- 最基本的敌兵布阵C++实现杭电1166
- PullToRefreshListView不请求网络
- [ZJOI2006]物流运输 luogu.P1772
- 出现GC overhead limit exceeded 的解决方案
- 如何让项目的start变成自己添加的character
- 数据结构实验之链表六:有序链表的建立
- Struts 2中<s:iterator>标签的用法
- MongoDB从小白开始使用到……