codeforces-scrape

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

63B.md (2336B)


      1 Source: https://codeforces.com/problemset/problem/63/B
      2 
      3 B. Settlers' Training
      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 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly *n* soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.
     22 
     23 Every soldier has a rank — some natural number from 1 to *k*. 1 stands for a private and *k* stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.
     24 
     25 To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the *n* soldiers are present.
     26 
     27 At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank *k* are present, exactly one soldier increases his rank by one.
     28 
     29 You know the ranks of all *n* soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank *k*.
     30 
     31 Input
     32 
     33 The first line contains two integers *n* and *k* (1 ≤ *n*, *k* ≤ 100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains *n* numbers in the non-decreasing order. The *i*-th of them, *a**i*, represents the rank of the *i*-th soldier in the defense building (1 ≤ *i* ≤ *n*, 1 ≤ *a**i* ≤ *k*).
     34 
     35 Output
     36 
     37 Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 4 4  
     45 1 2 2 3
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 4
     52 ```
     53 
     54 Input
     55 
     56 ```
     57 4 3  
     58 1 1 1 1
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 5
     65 ```
     66 
     67 Note
     68 
     69 In the first example the ranks will be raised in the following manner:
     70 
     71 1 2 2 3  →  2 2 3 4  →  2 3 4 4  →  3 4 4 4  →  4 4 4 4
     72 
     73 Thus totals to 4 training sessions that require 4 golden coins.