zoukankan      html  css  js  c++  java
  • Codeforces 875B 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.

    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.

    Example
    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.

    题意:

    题意是真的难懂,不过结合note好懂一些。每次操作在第i个位置放硬币,从左往右看,如果第i个位置有硬币,第i+1位置没有,则交换硬币(XOOO  →  OOOX位置换了3次,视为一次操作。X0X0->0X0X是换了两次硬币,从左往右看是一步操作)。

    题解:

    在末尾连续的硬币数是不用动的,则需要操作的次数一定是前面硬币的个数+1。

    #include<iostream>
    using namespace std;
    const int maxn=3e5+5;
    int a[maxn];
    int main()
    {
    	ios::sync_with_stdio(false);
    	int n;
    	cin>>n;
    	int tmp=n+1;
    	cout<<'1';
    	for(int i=1;i<=n;i++)
    	{
    		int x;
    		cin>>x;
    		a[x]=1;
    		while(a[tmp-1])
    			tmp--;
    		cout<<" "<<i+tmp-n;
    	}
    	cout<<endl;
    	return 0;
    }
    
  • 相关阅读:
    【海洋女神原创】安装导论谨以此文与那些徘徊在安装的大门外,并且被花哨的功能所迷惑的朋友们共勉。
    【海洋女神原创】Installshield脚本拷贝文件常见问题汇总
    Installshield获取安装包版本的系统变量是IFX_PRODUCT_VERSION
    Installshield 64位操作系统下拷贝文件,如何重定向到32位的系统文件夹下
    【海洋女神原创】installshield 32位打包和64位打包的注意事项
    Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法
    【海洋女神原创】一个简单的带序列号输入的安装包
    How to:installshield安装包怎样才能出现选择路径的界面?
    Excel连接字符串在.NET中的应用
    将Excel的数据导入DataGridView中[原创]
  • 原文地址:https://www.cnblogs.com/orion7/p/7750162.html
Copyright © 2011-2022 走看看