There are nnn boxes with different quantities of candies in each of them. The iii-th box has aia_iai​ candies inside.

You also have nnn friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don’t want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.

What’s the minimum total number of candies you have to eat to satisfy the requirements? Problem tags :greedy, math,sorting, *800.

Input:The first line contains an integer t(1≤t≤1000)t (1≤t≤1000)t(1≤t≤1000) — the number of test cases. The first line of each test case contains an integer n(1≤n≤50)n (1≤n≤50)n(1≤n≤50) — the number of boxes you have. The second line of each test case contains nnn integers a1,a2,…,an(1≤ai≤107)a_1,a_2,…,a_n (1≤a_i≤10^7)a1​,a2​,…,an​(1≤ai​≤107) — the quantity of candies in each box.

Output:For each test case, print a single integer denoting the minimum number of candies you have to eat to satisfy the requirements.

5
5
1 2 3 4 5
6
1000 1000 5 1000 1000 1000
10
1 2 3 5 1 2 7 9 13 5
3
8 8 8
1
10000000
10
4975
38
0
0

Note:For the first test case, you can eat 111
candy from the second box, 222 candies from the third box, 333 candies from the fourth box and 444 candies from the fifth box. Now the boxes have [1,1,1,1,1][1,1,1,1,1][1,1,1,1,1] candies in them and you ate 0+1+2+3+4=100+1+2+3+4=100+1+2+3+4=10 candies in total so the answer is 101010.

For the second test case, the best answer is obtained by making all boxes contain 555 candies in them, thus eating 995+995+0+995+995+995=4975995+995+0+995+995+995=4975995+995+0+995+995+995=4975 candies in total.

题意不再赘述,都是简单英文。主要是学习下如何拓宽思路。有两种常用解法。一求最小值有两种方法新人喜欢sort,实际上可以在每次输入时使用min保存最小值。
下面分别是解法1/2的C++实例代码,节省精力就不添加注释了:

#include<bits/stdc++.h>
using namespace std;int main() {int t; cin>>t;while(t--) { int sum = 0;int n;cin >> n;vector <int> a(n);for(int i = 0; i < n; i++) {cin >> a[i];sum += a[i];}sort(a.begin(),a.end());cout << sum - a[0] * n << endl;}
}
#include "bits/stdc++.h"
using namespace std;int main() {int t; cin >> t;while(t--) {int n; cin >> n;vector<int> a(n);int mn = INT_MAX, ans = 0;for(int i = 0; i < n; ++i) {cin >> a[i];mn = min(mn, a[i]);}for(int i = 0; i < n; ++i) {ans += a[i] - mn;}cout << ans << "\n";}
}

B. Equal Candies相关推荐

  1. B - Equal Candies CodeForces - 1676B

    There are nn boxes with different quantities of candies in each of them. The ii-th box has a_ia i ​ ...

  2. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  3. java equal hashcode_Java(二)equal 和 hashcode使用

    一.hashCode()和equals()是什么? hashCode()方法和equals()方法的作用其实一样,在Java里都是用来对比两个对象是否相等一致. Equal 没有重写的equal 使用 ...

  4. 讨论JDK的File.equal()

    我们一般比较两个文件中的对象是相同的文件,通常使用java.io.File.equal().这里,equal()是不是文件内容的比较结果为.象是否指向同一个文件. File的equal()方法.实际上 ...

  5. RuntimeError: each element in list of batch should be of equal size

    RuntimeError: each element in list of batch should be of equal size 示例代码: import os import re from t ...

  6. K - Candies POJ - 3159(利用了自定义比较操作符)

    K - Candies POJ - 3159 题意: 孩子 A 觉得 B 得到的糖果不能比自己多超过 c,求 n 比 1 最多能多几颗糖果 思路:DJ,松弛条件: sweet[A] > swee ...

  7. 语义分割--Not All Pixels Are Equal:Difficulty-Aware Semantic Segmentation via Deep Layer Cascade

    Not All Pixels Are Equal: Difficulty-Aware Semantic Segmentation via Deep Layer Cascade CVPR2017 本文针 ...

  8. logic:equal 标签的使用(转)

    <logic:equal name="music" property="distrubute" value="true">    ...

  9. 记录由Equal基础知识引起的内存泄露

    在最近的公司框架开发中,利用了网上某大牛的反射缓存库作为辅助.在测试的时候发现出现了巨大的内存泄露,在频繁的操作后,内存不断的产生巨大的开销,10多分钟就占有了5,6m的内存.解决问题的时,公司不能上 ...

最新文章

  1. 【Fragment】onActivityResult
  2. The Linux Command Line读书笔记(二)
  3. java中的wait和sleep_java中wait和sleep的区别
  4. poj 1821(单调队列优化dp)
  5. 欧几里得范数_从范数到正则化
  6. P2408- 不同子串个数【SA】
  7. Linux下开发常用的CVS使用手册
  8. php团队规范,PHP团队 编码规范 代码样式风格规范
  9. 学会 配置文件+反射,走遍全球都不怕.
  10. orm php 自定义,Eloquent ORM 自定义 builder
  11. 我的世界可以在服务器用修改器,我的世界TMI修改器使用教程
  12. 大话移动通信(第2版)!(文末赠书福利)
  13. 三阶矩阵的lu分解详细步骤_lu分解(笔算矩阵lu分解步骤)
  14. 您需要来自Administration的权限才能对此文件夹进行更改’怎么删除文件,window10删除2345流氓软件
  15. confluence开发,实现与现有单点登录sso系统对接。
  16. 阿⾥云Apsara Clouder云计算专项技能认证:云服务器ECS⼊门【 个⼈所得税年度应纳税额抵扣 3600(0成本)】
  17. 【C语言入门】笔记八 (数组中)
  18. You Can’t Future-Proof Solutions
  19. Jenkins基础:API:10:使用API更新进行节点的连接和断开
  20. 如何打开一个嵌在PC端应用的web窗口的控制台?

热门文章

  1. sqlserver 返回当月最后一天,上个月最后一天,下个月最后一天
  2. 在Linux下adb连接不上android手机的终极解决方案
  3. how to print picture
  4. 神经网络模型matlab例子,神经网络算法matlab
  5. D3.js的v5版本入门教程(第五章)—— 选择、插入、删除元素
  6. 全程中文!谷歌发布机器学习速成课,完全免费(附视听评测)
  7. 我的世界最后战线怎么下java_我的世界最后战线整合包
  8. MyeclipseTFS团队资源管理器插件
  9. 全栈项目|小书架|微信小程序-实现搜索功能
  10. 11. 查询表student——查询名字中第2个字为阳字的学生的姓名和学号。