codeforces-479B-Towers
2015-11-07 12:47
405 查看
codeforces-479B-Towers
[code] time limit per test1 second memory limit per test256 megabytes
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.
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.
题目链接:cf-479B
题目大意:有很多个塔,问最少的移动步数使得最高塔和最低塔高度差最小
题目思路:min_element(name,name+n)-name // 返回的是一个数组最小值的下标
*min_element(name,name+n) //返回最小的值
暴力,找到最大和最小高度进行调整
以下是代码:
[code]#include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> using namespace std; int a[200]; int ans[2000]; int main(){ int n,k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } int m = 0; int maxn = 0,minn = 0; while(k--) { maxn = max_element(a,a + n) - a; minn = min_element(a,a + n) - a; if (a[maxn] - a[minn] > 1) { ans[m++] = maxn + 1; ans[m++] = minn + 1; a[maxn]--; a[minn]++; } } maxn = *max_element(a,a + n); minn = *min_element(a,a + n); cout << (maxn - minn) << " " << (m / 2) << endl; for (int i = 0; i < m; i += 2) { cout << ans[i] << " " << ans[i + 1] << endl; } return 0; }
相关文章推荐
- html5兼容IE
- 安装最新的qt5.5.1,在使用qmake -v 出现qmake -v could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No su
- 安全参透之旅第3章 Metasploit工具 第一节
- CCPC 2015 A题
- 逆水行舟问题
- Linux Scheduling Domains
- hdu 2037 今年暑假不AC
- hdu 2037 今年暑假不AC
- cookie记录用户的浏览商品的路径
- manacher算法详解
- 分解质因数
- ORACLE 中的 ROW_NUMBER() OVER() 分析函数的用法
- Map<K,V>
- (异常分析)实例化Configuration 的时候提示:Cannot instantiate the type Configuration
- ps 和 kill 结合使用
- OkHttp解析系列-开篇
- IDF 聪明的小羊 栅栏密码
- SELinex权限问题调试
- RBAC权限管理
- [279]Perfect Squares