codechef2013月赛1月 The Minimum Number Of Moves 使全部数相等的最小步数

来源:互联网 发布:linux添加路由 编辑:程序博客网 时间:2024/06/05 07:36

The Minimum Number Of Moves

Problem code: SALARY

  • Submit
  • My Submissions
  • All Submissions

Little chief has his own restaurant in the city. There are N workers there. Each worker has his own salary. The salary of thei-th worker equals toWi (i =1, 2, ...,N). Once, chief decided to equalize all workers, that is, he wants to make salaries of all workers to be equal. But for this goal he can use only one operation: choose some worker and increase by 1 salary of each worker, except the salary of the chosen worker. In other words, the chosen worker is the loser, who will be the only worker, whose salary will be not increased during this particular operation. But loser-worker can be different for different operations, of course. Chief can use this operation as many times as he wants. But he is a busy man. That's why he wants to minimize the total number of operations needed to equalize all workers. Your task is to find this number.

Input

The first line of the input contains an integer T denoting the number of test cases. The description ofT test cases follows. The first line of each test case contains a single integerN denoting the number of workers. The second line contains N space-separated integersW1, W2, ...,WN denoting the salaries of the workers.

Output

For each test case, output a single line containing the minimum number of operations needed to equalize all workers.

Constraints

  • 1T100
  • 1N100
  • 0Wi10000 (104)

Example

Input:231 2 3242 42Output:30

Explanation

Example Case 1. Chief can equalize all salaries in 3 turns:

Turn IDIDs of involved workersSalaries after the move11 22 3 321 23 4 331 34 4 4

Example Case 2. All salaries are already equal. He doesn't need to do anything.




http://www.codechef.com/JAN13/problems/SALARY
题意:  输入cas个数 输入n 之后n个数 每次选择一个最大的    然后除了最大的一个外 其它的全部加1  问最少需要多少步能够使得所有数相等


思路:
直接暴力是不行的 
可以根据题目所给条件去认真分析  注意分析题目所给的每一个条件    找出问题的特性
这题中 可以看出 假设一开始输入的n个数和为sum其中最小的数在pos处   当最后相等时 为x 则  x*n-sum=(n-1)*(x-a[pos])
根据条件推出上面的式子    

#include<stdio.h>int a[105];int main(){int i,cas,n,pos,ans,sum,min;scanf("%d",&cas);while(cas--){min=999999;         scanf("%d",&n); sum=0; for(i=0;i<n;i++)  {scanf("%d",&a[i]);sum+=a[i];} ans=0;pos=0; for(i=0;i<n;i++) if(a[i]<min) {min=a[i];pos=i;} int flag=1;  for(i=0;i<n;i++)   {  if(a[i]!=min) break;  }  if(i==n) { printf("0\n");continue;} while(1) { min++; if((min*n-sum)==(min-a[pos])*(n-1))  break;  }// if(min!=a[pos]) printf("%d\n",min-a[pos]); }return 0;}











          
原创粉丝点击