codeforces-scrape

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

864D.md (2377B)


      1 Source: https://codeforces.com/problemset/problem/864/D
      2 
      3 D. Make a Permutation!
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Ivan has an array consisting of *n* elements. Each of the elements is an integer from 1 to *n*.
     22 
     23 Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to *n* was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
     24 
     25 Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
     26 
     27 In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations *x* and *y*, then *x* is lexicographically smaller if *x**i* < *y**i*, where *i* is the first index in which the permutations *x* and *y* differ.
     28 
     29 Determine the array Ivan will obtain after performing all the changes.
     30 
     31 Input
     32 
     33 The first line contains an single integer *n* (2 ≤ *n* ≤ 200 000) — the number of elements in Ivan's array.
     34 
     35 The second line contains a sequence of integers *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ *n*) — the description of Ivan's array.
     36 
     37 Output
     38 
     39 In the first line print *q* — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with *q* changes.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 4  
     47 3 2 2 3
     48 ```
     49 
     50 Output
     51 
     52 ```
     53 2  
     54 1 2 4 3
     55 ```
     56 
     57 Input
     58 
     59 ```
     60 6  
     61 4 5 6 3 2 1
     62 ```
     63 
     64 Output
     65 
     66 ```
     67 0  
     68 4 5 6 3 2 1
     69 ```
     70 
     71 Input
     72 
     73 ```
     74 10  
     75 6 8 4 6 7 1 6 3 4 5
     76 ```
     77 
     78 Output
     79 
     80 ```
     81 3  
     82 2 8 4 6 7 1 9 3 10 5
     83 ```
     84 
     85 Note
     86 
     87 In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.
     88 
     89 In the second example Ivan does not need to change anything because his array already is a permutation.