Codeforces Round #274 (Div. 2) 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次操作,每次操作是从一个塔上拿走一层到另个塔上,让你求的是使最高的塔-最低的塔的值最小。

做法:我是用结构体做的,用id,x来表示一个塔的id和高度,在定义kk来记录操作数,然后按照高度进行排序,如果ans=最高-最低=0则直接输出(0 0),不过不为0则直接进行k次操作(题目没有说要最小),每次操作都是排序一下,然后最大值-1,最小值+1,kk++,并把两个值的id记录到jl数组中,如果ans=0了则退出循环。最后输出ans,kk就行,然后输出kk行记录的值。

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL  long long
#define inf 0x0f0f0f0f
using namespace std;
typedef pair<int,int> pp;
struct number
{
    int id;
    int x;
}num[1005];
bool cmp(struct number a,struct number b)
{
    return a.x<b.x;
}
int main()
{
    int n,m,i;
    int ans,cz;
    pp jl[1005];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(jl,0,sizeof(jl));
        memset(num,0,sizeof(num));
        cz=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&num[i].x);
            num[i].id=i;
        }
        int kk=0;
        while(1)
        {
            sort(num,num+n,cmp);
            ans=num[n-1].x-num[0].x;
            if(ans==0)
                break;
            if(m==0)
                break;
            num[0].x+=1;
            num[n-1].x-=1;
            m--;
            jl[kk++]=make_pair(num[n-1].id,num[0].id);
        }
        printf("%d %d\n",ans,kk);
        for(i=0;i<kk;i++)
            printf("%d %d\n",jl[i].first+1,jl[i].second+1);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值