题目链接: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 mmy){ mmy = t[i].y; mmx = t[i].x; } } if(mmy - my <br> <br> <p></p> <p><br> </p> <p><br> </p> <p><br> </p> </time.h></stdlib.h></math.h></string></map></set></queue></vector></algorithm></iostream></string.h></stdio.h>

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to design the dotted line segmentation effect in the menu? When designing menus, it is usually not difficult to align left and right between the dish name and price, but how about the dotted line or point in the middle...

HTML Element Analysis in Web Code Editor Many online code editors allow users to enter HTML, CSS, and JavaScript code. Recently, someone proposed...

About how to avoid code compression when building static pages using react-app-rewired Many developers want to deliver to...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use