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

简介:

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
题意: 给出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;
}




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

本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4657532.html,如需转载请自行联系原作者


相关文章
|
4月前
Codeforces Round #567 (Div. 2)
【7月更文挑战第1天】
44 7
|
人工智能 算法 BI
Codeforces Round #179 (Div. 2)A、B、C、D
我们每次加进来的点相当于k,首先需要进行一个双重循环找到k点和所有点之间的最短路径;然后就以k点位判断节点更新之前的k-1个点,时间复杂度降到O(n^3),而暴力解法每次都要进行floyd,时间复杂度为O(n^4);相比之下前述解法考虑到了floyd算法的性质,更好了运用了算法的内质。
54 0
|
机器学习/深度学习 人工智能
Codeforces Round 889 (Div. 2)
Codeforces Round 889 (Div. 2)
154 0
|
机器学习/深度学习 Go
codeforces round 885 (div. 2)
codeforces round 885 (div. 2)
95 0
|
人工智能 算法