Home > Article > Web Front-end > Codeforces Round #274 (Div. 2) B Towers_html/css_WEB-ITnose
题目链接:Towers
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
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
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.
大致题意:有n个塔,存在这样一种操作:从最高的塔上拿掉一层,放到其他任意塔上去。 现给出n,给出操作允许执行的最大次数k,问经过操作之后,最高塔和最低塔之间的高度差最小为多少,并把移动方法,打印出来。
解题思路:本来想用map写的,后来想想不太好搞,然后又看了看数据,不大,直接暴力就搞了。开个循环,当最高塔和最低塔的高度差值小于2时,就退出(因为已经找到了最优解了,小于2时,如果再移动,就会重复,并不比当前状态好)。每次操作都从最高塔的高度减一,最低塔的高度加一,并分别记录最高塔和最低塔的编号,同时纪录操作次数t,当t >= k时,退出。主要的时间主要浪费在找最大塔和最低塔的位置,只要遍历一遍数组,即可。
AC代码:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7fffffffconst int maxn = 1005;typedef struct{ int x,y;}T;T t[maxn], tt[maxn];int main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int n,k,ch; while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1; i<=n; i++){ scanf("%d", &ch); t[i].x = i; t[i].y = ch; } memset(tt,0,sizeof(tt)); int my, mx, mmy, mmx; int cnt = 0; while(1){ my = 1234567; mx = 0; mmy = -1234567; mmx = 0; for(int i=1; i<=n; i++){ if(t[i].y < my){ my = t[i].y; mx = t[i].x; } if(t[i].y > mmy){ mmy = t[i].y; mmx = t[i].x; } } if(mmy - my < 2 || k<1) break; else{ tt[cnt].x = mmx; tt[cnt++].y = mx; t[mmx].y --; t[mx].y ++; k --; } } printf("%d %d\n", mmy - my, cnt); if(cnt){ for(int i=0; i<cnt; i++) printf("%d %d\n", tt[i].x, tt[i].y); printf("\n"); } } return 0;}