• Codeforces Round #274 (Div. 2)-B. Towers


    http://codeforces.com/contest/479/problem/B

    B. Towers
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 ai 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).

    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.

    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.

    Input

    The first line contains two space-separated positive integers n and k (1 ≤ n ≤ 1001 ≤ 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 ai (1 ≤ ai ≤ 104) — the towers' initial heights.

    Output

    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.

    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.

    If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.

    Sample test(s)
    input
    3 2
    5 8 5
    output
    0 2
    2 1
    2 3
    input
    3 4
    2 2 4
    output
    1 1
    3 2
    input
    5 3
    8 3 2 6 3
    output
    3 3
    1 3
    1 2
    1 3
    Note

    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.

    解题思路:n座塔,每次从最高的那座塔往最低的那座塔移动一个,最多可以移动k次,问最少移动多少次后最高的塔与最低的塔的高度差最小,并输出移动顺序

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main(){
     5     int n, k, max, min, i, j, maxflag, minflag;
     6     int a[110];
     7     int from[1010], to[1010];
     8     while(scanf("%d %d", &n, &k) != EOF){
     9         for(i = 1; i <= n; i++){
    10             scanf("%d", &a[i]);
    11         }
    12         for(i =1 ; i <= k; i++){
    13             max = min = a[1];
    14             maxflag = minflag = 1;
    15             for(j = 2; j <= n; j++){
    16                 if(a[j] > max){
    17                     max = a[j];
    18                     maxflag = j;
    19                 }
    20                 if(a[j] < min){
    21                     min = a[j];
    22                     minflag = j;
    23                 }
    24             }
    25             if(a[maxflag] - 1 == a[minflag] || max == min){ //注意
    26                 break;
    27             }
    28             from[i] = maxflag;
    29             to[i] = minflag;
    30             a[maxflag]--;
    31             a[minflag]++;
    32         }
    33 
    34         max = min = a[1];
    35         for(j = 2; j <= n; j++){
    36             if(a[j] > max){
    37                 max = a[j];
    38             }
    39             if(a[j] < min){
    40                 min = a[j];
    41             }
    42         }
    43         i -= 1;
    44         printf("%d %d ", max - min, i);
    45         for(j = 1; j <= i; j++){
    46             printf("%d %d "from[j], to[j]);
    47         }
    48     }
    49     return 0;

    50 } 

  • 相关阅读:
    作品-系统-[原生开发]新蓝景CRM管理系统
    作品-网站-[二次开发]汇名惠商城
    js声明json数据,打印json数据,遍历json数据,转换json数据为数组
    js 鼠标滚动到某屏时,加载那一屏的数据,仿京东首页楼层异步加载模式
    js转换数据类型为浮点型,并取两位小数点
    php判断是否是微信客户端的浏览器访问
    百度地图API地理位置和坐标转换
    php 对象中连贯执行方法
    php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容
    php 获取远程图片保存到本地
  • 原文地址:https://www.cnblogs.com/angle-qqs/p/4044703.html
Copyright © 2020-2023  润新知