Codeforces 479B. Towers, codeforces479b

Source: Internet
Author: User

Codeforces 479B. Towers, codeforces479b


Pure violence .....

B. Towerstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

As you know, all the kids in Berland love playing with cubes. Little Petya hasNTowers consisting of cubes of the same size. Tower with numberIConsistsAICubes 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 wowould 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 moreKSuch 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 integersNAndK(1 digit ≤ DigitNLimit ≤ limit 100, 1 limit ≤ limitKLimit ≤ limit 1000)-the number of towers in the given set and the maximum number of operations Petya can perform. The second line containsNSpace-separated positive integersAI(1 digit ≤ DigitAILimit ≤ limit 104)-the towers 'initial heights.

Output

In the first line print two space-separated non-negative integersSAndM(MLimit ≤ limitK). The first number is the value of the minimum possible instability that can be obtained after loading at mostKOperations, the second number is the number of operations needed for that.

In the nextMLines print the description of each operation as two positive integersIAndJ, Each of them lies within limits from 1N. They represent that Petya took the top cube fromI-Th tower and put in onJ-Th one (I  =J). Note that in the process of executing Ming 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.




#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;int n,k;struct TA{    int x,id;}ta[200];bool cmp(TA a,TA b){    return a.x<b.x;}vector<int> ans;int main(){    scanf("%d%d",&n,&k);    for(int i=1;i<=n;i++)    {        int x;        scanf("%d",&x);        ta[i].x=x; ta[i].id=i;    }    while(k--)    {        sort(ta+1,ta+1+n,cmp);        int diff=ta[n].x-ta[1].x;        if(diff==0) break;        else        {            ta[n].x--; ta[1].x++;            ans.push_back(ta[n].id);            ans.push_back(ta[1].id);        }    }    sort(ta+1,ta+1+n,cmp);    int diff=ta[n].x-ta[1].x;    int sz=ans.size();    printf("%d %d\n",diff,sz/2);    for(int i=0;i<sz;i+=2)    {        printf("%d %d\n",ans[i],ans[i+1]);    }    return 0;}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.