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 ≤ 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.

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

题意:给出n个塔,由a[i]个砖块堆起来。整个体系的稳定系数为:最高的高度减去最矮的高度。

能够移动k次,求移动k次中,能够得到的最低的稳定系数,以及怎样移动,每次仅仅能移动1个方块。

思路:模拟

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005; struct Node {
int x, id;
} node[maxn];
struct Op {
int x, y;
} q[maxn];
int ans[maxn]; bool cmp(Node a, Node b) {
return a.x < b.x;
} int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &node[i].x);
node[i].id = i;
}
sort(node+1, node+1+n, cmp);
ans[0] = node[n].x - node[1].x;
for (int i = 1; i <= k; i++) {
sort(node+1, node+1+n, cmp);
node[1].x++, node[n].x--;
q[i].y = node[1].id, q[i].x = node[n].id;
sort(node+1, node+1+n, cmp);
ans[i] = node[n].x - node[1].x;
}
int pos = 0, Max = ans[0];
for (int i = 1; i <= k; i++)
if (ans[i] < Max)
Max = ans[i], pos = i;
printf("%d %d\n", Max, pos);
for (int i = 1; i <= pos; i++)
printf("%d %d\n", q[i].x, q[i].y);
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces Round #274 (Div. 2) B. Towers的更多相关文章

  1. Codeforces Round #274 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...

  2. Codeforces Round #274 (Div. 2)

    A http://codeforces.com/contest/479/problem/A 枚举情况 #include<cstdio> #include<algorithm> ...

  3. Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...

  4. Codeforces Round #274 (Div. 1) B. Long Jumps 数学

    B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...

  5. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  6. codeforces水题100道 第八题 Codeforces Round #274 (Div. 2) A. Expression (math)

    题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #inclu ...

  7. Codeforces Round #274 (Div. 2)-C. Exams

    http://codeforces.com/contest/479/problem/C C. Exams time limit per test 1 second memory limit per t ...

  8. Codeforces Round #274 Div.1 C Riding in a Lift --DP

    题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种 ...

  9. Codeforces Round #274 (Div. 2) E. Riding in a Lift(DP)

    Imagine that you are in a building that has exactly n floors. You can move between the floors in a l ...

随机推荐

  1. iOS国际化和genstrings所有子文件夹本地化字符串

    iOS国际化和genstrings所有子文件夹本地化字符串 在最近的一个繁忙的对外工程.每天加班.没有时间更新博客.简单谈一下知识的国际化. 首先,我们使用串.必须NSLocalizedString( ...

  2. KMP算法---字符串匹配

    算法细节详见点击打开链接和点击打开链接 #include <stdio.h> #include <stdlib.h> #define N 7 #define M 15 void ...

  3. .NET/C# RabbitMQ

    本系列文章均来自官网原文,属于个人翻译,如有雷同,权当个人归档,忽喷. RabitMQ 是一个消息中间件,其实就是从消息生产者那里接受消息,然后发送给消息消费者.在这个传输过程中,可以定义一些缓存,持 ...

  4. uva10635 LCS映射转LIS

    题目给定 2个序列,要我们求LCS,但是序列的长度最长是250*250, LCS的时间复杂度是O(N*N),所以无法解决 我们可以第一个序列的数字,按位置,映射为1.2.3.4.5.6.7.8.9 那 ...

  5. 垂死或涅槃重生 -- Delphi XE5 我们将宣布感情的回归

    Delphi 在很大程度上是一个被遗忘我的工具. 无论是在使用RapidSql , 我还没有收到Embarcadero 本公司发行参与邀请Delphi XE5该公告将. 可能有人会问,为什么Embar ...

  6. Android在ExpandableListView控制的基本使用

    在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...

  7. 轻松管理您的网络password

    在互联网在现在这个时代,,我们注册了很多帐户.支付宝账号password,各种宝账户password.微信,QQ,电话password,购买各种网站,金融password,它是不是让孩子们的鞋子瞬间淡 ...

  8. Xamarin之 环境错误集锦

    错误信息:   connection of the layout renderer failed.this may be caused by a misconfiguration of java .p ...

  9. 十步完全理解SQL(转)

    本文由 伯乐在线 - 水果泡腾片 翻译.未经许可,禁止转载!英文出处:Lukas Eder.欢迎加入翻译组. 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同 ...

  10. 图解Http协议 (转)

    一.技术基石及概述 问:什么是HTTP? 答:HTTP是一个客户端和服务器端请求和响应的标准TCP.其实建立在TCP之上的. 当我们打开百度网页时,是这样的: https://www.baidu.co ...