题目链接:https://www.nowcoder.com/acm/contest/90/J
题目描述
牛客网是IT求职神器,提供海量C++、JAVA、前端等职业笔试题库,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的编程。作为acmer的小A,牛客网是他首选的题库。
小A是一个中度强迫症患者,每次做数组有关的题目都异常难受,他十分希望数组的每一个元素都一样大,这样子看起来才是最棒的,所以他决定通过一些操作把这个变成一个看起来不难受的数组,但他又想不要和之前的那个数组偏差那么大,所以他每次操作只给这个数组的其中n-1个元素加1,但是小A并不能很好的算出最优的解决方案,如果你能帮他解决这个问题,小A就能送你一个气球
输入描述:
第一行一个整数T(T<=100),表示组数
对于每组数据有一个n,表示序列的长度(0 < n < 100000)
下面一行有n个数,表示每个序列的值(0 < ai < 1000)
输出描述:
输出两个数
第一个数表示最小的操作步数
第二个数经过若干步以后的数组元素是什么
示例1
输入
1
3
1 2 3
输出
3 4
Description:
给出一个数组,每一次可以选择 n-1 个数将它们全部加一,使得最后所有数都相等所需要的最少步数
Solution:
将 n-1 个数全部加1,其实就相当于将所有数都加1,然后再将其中一个数减去1。
那么问题就转化为了每次选一个数将其减一,求达到目的的最小操作数。
那么也就是各个数与最小值之差的和。
Code:
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define _rush() int T; cin >> T; for(int Case = 1; Case <= T; Case++)
#define rush() int T; scanf("%d", &T); for(int Case = 1; Case <= T; Case++)
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const int Mod = 1e9 + 7;
const int MaxN = 1e5 + 5;
int a[MaxN];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t; cin >> t;
while(t--) {
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
int ans = 0;
for(int i = 1; i <= n; i++) ans += a[i] - a[1];
cout << ans << " " << a[1] + ans << endl;
}
return 0;
}