codechef2013月赛1月 The Minimum Number Of Moves 使全部数相等的最小步数
来源:互联网 发布:linux添加路由 编辑:程序博客网 时间:2024/06/05 07:36
The Minimum Number Of Moves
Problem code: SALARY
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
- 1 ≤ T ≤ 100
- 1 ≤ N ≤ 100
- 0 ≤ Wi ≤ 10000 (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 4Example 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;}
- codechef2013月赛1月 The Minimum Number Of Moves 使全部数相等的最小步数
- Minimum Moves to Equal Array Elements II最小移动步数问题解法详解
- 453. Minimum Moves to Equal Array Elements | 计算步数
- Children of the Candy Corn(图的遍历bfs最小步数)
- hdu1394 Minimum Inversion Number(最小逆序数)
- *[Lintcode]Interval Minimum Number 区间最小数
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- 一个数转化成1的最小步数
- 权重 相等大于N的最小数
- 大二第二次月赛--The number of maximum subset
- 453. Minimum Moves to Equal Array Elements移动最小的步数使所有元素相等
- (hdu step 4.2.3)Knight Moves(求从起点是否能够到达终点的最小步数)
- leetcode_447. Number of Boomerangs 平面上距离相等的点的组数
- 最小步数
- 最小步数
- nyoj最小步数
- 最小步数
- nyoj58最小步数
- UVA 465 - Overflow (浮点数的优势)
- wordpress上线时候链接地址依然指向本地的问题
- JQuery获取浏览器窗口宽高
- 微博传播在企业品牌营销中的优势
- 散列相关
- codechef2013月赛1月 The Minimum Number Of Moves 使全部数相等的最小步数
- C++输入一行字符串的一点小结
- 程序员的学习和积累
- 如何识别真正的程序员
- vc++ 使用菜单类动态创建菜单
- Shadow Map在DirectX9.0 SDK Sample 的实现方法
- Bdsyn百度手机助手是何物,它是如何神不知鬼不觉地安装到你的电脑里的?
- Hibernate二级缓存攻略
- 自定义菜单