[Codeforces479B] Towers

描述 Description

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.

输入格式 InputFormat

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 ai (1 ≤ ai ≤ 104) — the towers’ initial heights.

输出格式 OutputFormat

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.

样例输入 SampleInput

4 6
1 10 8 2

样例输出 SampleOutput

2 6
2 1
2 1
2 4
3 1
2 4
3 1


Codeforces 479B


代码 Code

每次找最大最小值调整就好了。

#include <stdio.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int inf=0x7fffffff/27.11;
int i,j,t,n,m,l,r,k,z,y,x;
int a[105],mina,maxa;
vector < pair<int,int> > v;
inline void find()
{
    int i,j,t;
    mina=maxa=1;
    for (i=1;i<=n;i++) 
    {
        if (a[mina]>a[i]) mina=i;
        if (a[maxa]<a[i]) maxa=i;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    k=m;
    while (k--)
    {
        find();
        if (a[maxa]-a[mina]<2) break;
        a[maxa]-=1;
        a[mina]+=1;
        v.push_back(make_pair(maxa,mina));
    }
    find();
    printf("%d %d\n",a[maxa]-a[mina],v.size());
    for (i=0;i<v.size();i++) printf("%d %d\n",v[i].first,v[i].second);
    return 0;
}