Codeforces Round #274 (Div. 2) B. Towers_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:56:00
原创
1335人浏览过

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.

仿B站视频帧预览插件
仿B站视频帧预览插件

仿B站视频帧预览插件

仿B站视频帧预览插件 49
查看详情 仿B站视频帧预览插件

Sample test(s)

input

3 25 8 5
登录后复制

output

0 22 12 3
登录后复制

input

3 42 2 4
登录后复制

output

1 13 2
登录后复制

input

5 38 3 2 6 3
登录后复制

output

3 31 31 21 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;}
登录后复制




HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号