2021-08-19 AcWing 3809. 修改数组
输入样例:
2
3
10 1 4
5
1 1 2 2 3
输出样例:
3 7
2 0
巧用所给的数据范围,从1-100遍历判断即可
#include <iostream>
#include <limits.h>
using namespace std;
int a[1010];
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i =0 ; i < n; i++) cin >> a[i];
int minn = INT_MAX, res = 0;
for(int i = 1; i <= 100; i++)
{
int sum = 0;
for(int j = 0; j < n; j++)
{
if(a[j] > i + 1)
sum += a[j] - (i + 1);
else if(a[j] < i - 1)
sum += (i - 1) - a[j];
}
if(sum < minn) {
minn = sum;
res = i;
}
}
cout << res << " " << minn << endl;
}
return 0;
}
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799132.html