codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

875B.md (3374B)


      1 Source: https://codeforces.com/problemset/problem/875/B
      2 
      3 B. Sorting the Coins
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 For arranging coins Dima uses the following algorithm. One step of his algorithm looks like the following:
     24 
     25 1. He looks through all the coins from left to right;
     26 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.
     27 
     28 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.
     29 
     30 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.
     31 
     32 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.
     33 
     34 Input
     35 
     36 The first line contains single integer *n* (1 ≤ *n* ≤ 300 000) — number of coins that Sasha puts behind Dima.
     37 
     38 Second line contains *n* distinct integers *p*1, *p*2, ..., *p**n* (1 ≤ *p**i* ≤ *n*) — positions that Sasha puts coins in circulation to. At first Sasha replaces coin located at position *p*1, then coin located at position *p*2 and so on. Coins are numbered from left to right.
     39 
     40 Output
     41 
     42 Print *n* + 1 numbers *a*0, *a*1, ..., *a**n*, where *a*0 is a hardness of ordering at the beginning, *a*1 is a hardness of ordering after the first replacement and so on.
     43 
     44 Examples
     45 
     46 Input
     47 
     48 ```
     49 4  
     50 1 3 4 2
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 1 2 3 2 1
     57 ```
     58 
     59 Input
     60 
     61 ```
     62 8  
     63 6 8 3 4 7 2 1 5
     64 ```
     65 
     66 Output
     67 
     68 ```
     69 1 2 2 3 4 3 4 5 1
     70 ```
     71 
     72 Note
     73 
     74 Let's denote as O coin out of circulation, and as X — coin is circulation.
     75 
     76 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.
     77 
     78 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.
     79 
     80 XOOO  →  OOOX
     81 
     82 After replacement of the third coin, Dima's actions look this way:
     83 
     84 XOXO  →  OXOX  →  OOXX
     85 
     86 After replacement of the fourth coin, Dima's actions look this way:
     87 
     88 XOXX  →  OXXX
     89 
     90 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.