【CODEFORCES】 B. Towers
2015-08-07 19:16
351 查看
B. Towers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
output
input
output
input
output
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.
题解:这一题就是简单的模拟,直接模拟情况就可以了。
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
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.
题解:这一题就是简单的模拟,直接模拟情况就可以了。
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; int n,k,a[105],d[1005][2],minn,maxx,p,q,step,ans; int main() { scanf("%d%d",&n,&k); for (int i=1;i<=n;i++) scanf("%d",&a[i]); minn=a[1]; maxx=a[1]; p=1; q=1; for (int j=1;j<=n;j++) if (minn>a[j]) { minn=a[j]; p=j; } for (int j=1;j<=n;j++) if (maxx<a[j]) { maxx=a[j]; q=j; } if (maxx==minn) { printf("0 0\n"); return 0; } for (int i=1;i<=k;i++) { d[i][0]=q; d[i][1]=p; a[p]++; a[q]--; minn=a[1]; maxx=a[1]; p=1; q=1; for (int j=1;j<=n;j++) if (minn>a[j]) { minn=a[j]; p=j; } for (int j=1;j<=n;j++) if (maxx<a[j]) { maxx=a[j]; q=j; } if (i==1) { ans=maxx-minn; step=1; } else if (ans>maxx-minn) { ans=maxx-minn; step=i; } } minn=a[1]; maxx=a[1]; for (int i=1;i<=n;i++) minn=min(minn,a[i]); for (int i=1;i<=n;i++) maxx=max(maxx,a[i]); printf("%d %d\n",maxx-minn,step); for (int i=1;i<=step;i++) printf("%d %d\n",d[i][0],d[i][1]); return 0; }
相关文章推荐
- POJ-1581 A Contesting Decision-模拟ACM比赛时的评分规则
- C#高级编程七十九天----反射
- C++(11)标准I/O库
- springMVC获取request和response
- hdu 5358 First One(二进制+twopoint)
- POJ-1552 Doubles-二倍关系
- Docker初识
- (转)CSS中空格的写法
- SQL语句必会的50题
- (转)CSS中空格的写法
- 枚举需要的注意事项
- POJ-1338 Ugly Numbers-被2,3,5整除的数
- MATLAB中如何直接曲线拟合,而不使用cftool的GUI界面
- 软件测试中条件覆盖,路径覆盖,语句覆盖,分支覆盖的区别
- iphone休眠模式下,js定时器无法执行
- python 读写csv格式文件
- C++(10)函数
- 几篇关于Thread与AsyncTask的解析文章
- Matlab拟合工具箱的几个误差参数说明
- POJ-1005 I Think I Need a Houseboat-房屋淹没