codeforces-scrape

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

479B.md (2819B)


      1 Source: https://codeforces.com/problemset/problem/479/B
      2 
      3 B. Towers
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 As you know, all the kids in Berland love playing with cubes. Little Petya has *n* towers consisting of cubes of the same size. Tower with number *i* consists of *a**i* cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2).
     22 
     23 The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time.
     24 
     25 Before going to school, the boy will have time to perform no more than *k* such operations. Petya does not want to be late for class, so you have to help him accomplish this task.
     26 
     27 Input
     28 
     29 The first line contains two space-separated positive integers *n* and *k* (1 ≤ *n* ≤ 100, 1 ≤ *k* ≤ 1000) — the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains *n* space-separated positive integers *a**i* (1 ≤ *a**i* ≤ 104) — the towers' initial heights.
     30 
     31 Output
     32 
     33 In the first line print two space-separated non-negative integers *s* and *m* (*m* ≤ *k*). The first number is the value of the minimum possible instability that can be obtained after performing at most *k* operations, the second number is the number of operations needed for that.
     34 
     35 In the next *m* lines print the description of each operation as two positive integers *i* and *j*, each of them lies within limits from 1 to *n*. They represent that Petya took the top cube from the *i*-th tower and put in on the *j*-th one (*i* ≠ *j*). Note that in the process of performing operations the heights of some towers can become equal to zero.
     36 
     37 If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 3 2  
     45 5 8 5
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 0 2  
     52 2 1  
     53 2 3
     54 ```
     55 
     56 Input
     57 
     58 ```
     59 3 4  
     60 2 2 4
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 1 1  
     67 3 2
     68 ```
     69 
     70 Input
     71 
     72 ```
     73 5 3  
     74 8 3 2 6 3
     75 ```
     76 
     77 Output
     78 
     79 ```
     80 3 3  
     81 1 3  
     82 1 2  
     83 1 3
     84 ```
     85 
     86 Note
     87 
     88 In the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.