Codeforces Round #441(Div.2) D. Sorting the Coins 解题思路(模拟)

题目链接

D. Sorting the Coins
time limit per test:1 second
memory limit per test:512 megabytes
inputstandard input
outputstandard 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.

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个没有流通的硬币连成一排,不断选择其中一个硬币不流通,并用流通的硬币替换它n次。在此过程中,不断输出把流通硬币移到后端需要移动的次数。
解题思路
准确理解题意,理解之后就是模拟。每移动一次都可以把一个流通硬币移动到后端,但如果这个硬币本来就是最后的话就不需要移动了,所以需要一个指针来指向最后端位置,每遇到已经替换的位置,指针向前移动一个位置,移动次数减一。

#include <bits/stdc++.h>
#define Max_N 300000+5 
#define mod 10000019
typedef long long ll;
using namespace std;
int main(int argc, char const *argv[])
{
    int n;
    cin>>n;
    int flag[Max_N];
    memset(flag,0,sizeof(flag));
    int ans=1;
    int pos=n;
    for(int i=0;i<n;i++)
    {
        int x;
        if(i==0)
            cout<<"1";
        cin>>x;
        ans++;
        flag[x]=1;
        while(flag[pos]==1&&ans>1)
        {
            ans--;
            pos--;
        }
        cout<<" "<<ans;
    }
    cout<<endl;
    return 0;
}

总结
准确理解题意。

猜你喜欢

转载自blog.csdn.net/zrzzzlll/article/details/86593119
今日推荐