Salem gave you nn sticks with integer positive lengths a1,a2,…,ana1,a2,…,an.
For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from aa to bb is |a−b||a−b|, where |x||x| means the absolute value of xx.
A stick length aiai is called almost good for some integer tt if |ai−t|≤1|ai−t|≤1.
Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer tt and the total cost of changing is minimum possible. The value of tt is not fixed in advance and you can choose it as any positive integer.
As an answer, print the value of tt and the minimum cost. If there are multiple optimal choices for tt, print any of them.
Input
The first line contains a single integer nn (1≤n≤10001≤n≤1000) — the number of sticks.
The second line contains nn integers aiai (1≤ai≤1001≤ai≤100) — the lengths of the sticks.
Output
Print the value of tt and the minimum possible cost. If there are multiple optimal choices for tt, print any of them.
Examples
input
Copy
3 10 1 4
output
Copy
3 7
input
Copy
5 1 1 2 2 3
output
Copy
2 0
Note
In the first example, we can change 11 into 22 and 1010 into 44 with cost |1−2|+|10−4|=1+6=7|1−2|+|10−4|=1+6=7 and the resulting lengths [2,4,4][2,4,4]are almost good for t=3t=3.
In the second example, the sticks lengths are already almost good for t=2t=2, so we don't have to do anything.
题目大意:定义如果一个数为好数,则这个数| a[i] - t | <= 1.并且定义花费 = | a[i] - a[i]' | .给你n个数让你总这组数据中找出一个数字t并使这组数据都转化为满足条件的"好数",问最小话费为多少.
假设一个数为a[i],他的好数为a[i]',满足条件的数为t,所以| a[i]' - t | < 1即 t - 1 <= a[i]' <=t + 1.即a[i] - (t + 1) <= a[i] - a[i]' <= a[i] - (t - 1).| a[i] - a[i]' | = min(abs(a[i] - (t + 1),abs(a[i] - (t - 1))).
代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f
using namespace std;
int n,a[1010];
int main(){
cin >> n;
int i,j;
for(i = 1; i <= n; i++)
cin >> a[i];
int maxx = INF;
int sum = 0;
int t,ans;
for(i = 1; i <= 100; i++){
sum = 0;
for(j = 1; j <= n; j++){
if(abs(a[j] - i) > 1){
if(abs(a[j] - (1 + i)) <= abs(a[j] - (i - 1))) sum += abs(a[j] - (1 + i));
else sum += abs(a[j] - (i - 1));
}
else continue;
}
//cout << sum << endl;
if(sum < maxx){
t = i;
maxx = sum;
ans = sum;
}
}
cout << t << " " << ans << endl;
return 0;
}