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?≤?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;
}
时间: 2024-08-05 11:17:12

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

Codeforces Round #274 (Div. 2)

A. Expression 题意:给出a,b,c,给出"+","*",“()”在这三个数中任意放置这三个符号,求最大值 直接枚举6种情况就可以了,自己写的时候是挨个比找的最大值,后来发现别人的题解里面,直接将这6个值排序取最后一个数就可以了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include

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

题目链接:http://www.codeforces.com/problemset/problem/479/A题意:给你三个数a,b,c,使用+,*,()使得表达式的值最大.C++代码: #include <iostream> using namespace std; int a, b, c, ans; int main() { cin >> a >> b >> c; int t = max(a+b, a*b); ans = max(t + c, t * c

Codeforces Round #274 (Div. 2) E:Riding in a Lift DP + 前缀优化

题意: n,a,b,k(2?≤?n?≤?5000,1?≤?k?≤?5000,1?≤?a,?b?≤?n,a?≠?b).四个数.1到n的数,顺序排列,其实位置人在a位置而中心位置在b,人每次只能走一个点走动的距离必须小于|b?a|,人走k步之后停止,问人一共有多少种走法. 分析: 开始很容易想到一个深度优先搜索实现递归方法dfs(a, k)但k变为0就到达搜索底部,这样时间复杂度是O(nk)显然非常不好. 然后可以想到会有重算的情况,就可以加一个记忆优化把算过的(a,k)的二元组都保存下来.这样处理

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

Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several

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 lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take t

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

题意:给定n个楼层,初始在a层,b层不可停留,每次选一个楼层x,当|x-now| < |x-b| 且 x != now 时可达(now表示当前位置),此时记录下x到序列中,走k步,最后问有多少种可能的数的序列. 解法: 定义:      dp[i][j] 表示第i步在j楼的不同序列的个数 转移方程: 当j<b时, 那么dp[i][j] += dp[i-1][0~(j与b的中点(以下))] 当j>b时, 那么dp[i][j] += dp[i-1][(j与b的中点(以下))~n] 由于dp[

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #356 (Div. 2) [Codeforces680]

此处有目录↑ Codeforces Round #356(Div. 2):http://codeforces.com/contest/680 A. Bear and Five Cards (贪心) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A little bear Limak plays a game. He has

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i