codeforces 876 D. Sorting the Coins
1 second
512 megabytes
standard input
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:
- 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.
4
1 3 4 2
1 2 3 2 1
8
6 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的序列,开始都是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的更多相关文章
- codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))
题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...
- Codeforces D. Sorting the Coins
D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins
http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...
- Codeforces Round #441 D. Sorting the Coins(模拟)
http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...
- CodeForces - 876D Sorting the Coins
题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换.序 ...
- codeforces 876 C. Classroom Watch
http://codeforces.com/contest/876/problem/C C. Classroom Watch time limit per test 1 second memory l ...
- codeforces 876 F. High Cry(思维)
题目链接:http://codeforces.com/contest/876/problem/F 题解:一道简单的思维题,知道最多一共有n*(n+1)/2种组合,不用直接找答案直接用总的组合数减去不符 ...
- Codeforces Round #335 Sorting Railway Cars 动态规划
题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...
- Codeforces 911E - Stack Sorting
911E - Stack Sorting 思路: 用栈来模拟,能pop就pop,记下一个需要pop的数为temp,那么如果栈非空,栈顶肯定大于temp,那么加入栈 栈顶值-1 到 temp 的值,否则 ...
随机推荐
- “取件帮”微信小程序宣传视频链接及内容介绍
1.视频链接 视频上传至优酷自频道,地址链接:http://v.youku.com/v_show/id_XMzg2NTM3OTc5Ng==.html?spm=a2hzp.8253869.0.0 2.视 ...
- Beta阶段第二次网络会议
Beta阶段第二次网络会议 第一次会议问题解决情况 画面问题已经解决,游戏提示信息已加入完成 不同情况下背景已加入完成,但细节部分仍需要进行调整 科技树添加完成,权限修改完成,还存在部分细节调整 AI ...
- mysql数据库工具
1.navicat12 中文版及破解 链接:https://pan.baidu.com/s/1TH8m6lduHJybUGhmjFPIAA 提取码:kwcd 2.旧版本mysql-front(连接可选 ...
- Nginx 使用札记
nginx是什么? nginx是俄罗斯人 Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的一个十分轻量级的HTTP服务器.它是一个高性能的HTTP和反向代理服务器,同时也可以作 ...
- 第14章 Linux账号管理与ACL权限设置
Linux的账号与用户组 用户标识符:UID与GID 每一个文件都有一个所有者ID和用户组ID,当我们需要查看文件属性时,系统会根据/etc/passwd和/etc/group的内容,找到对应UID和 ...
- MySQL 基于xtrabackup备份—热备工具
xtrabackup(仅对InnoDB存储引擎支持热备) percona公司开发 改进的MySQL分支:percona-server 存储引擎改进:InnoDB —> XtraDB 使用本地的R ...
- PAT 甲级 1132 Cut Integer
https://pintia.cn/problem-sets/994805342720868352/problems/994805347145859072 Cutting an integer mea ...
- (转)Linux 命令--查看物理CPU个数、核数、逻辑CPU个数
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...
- delphi self 的使用
delphi之self 在使用delphi的对象技术的时候,经常会看到一个词汇:self,它到底指的是什么呢? 我们还要从对象与类的关系谈起. 类是对将要创建的对象的性质的描述,是一种文档.这很重要: ...
- uva1391-Astronauts
宇航员执行任务,有三个任务ABC.把宇航员按照平均年龄分成新老两组,老宇航员可以去AC,新宇航员可以取BC.宇航员之间有不能共存关系,问是否有合法的分配方案. 分析 虽然有三个任务,但每个宇航员还是只 ...