A. Salem and Sticks
Salem gave you nn sticks with integer positive lengths a1,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 .
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≤1000 ) — the number of sticks.
The second line contains nn integers aiai (1≤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 1 into 2 and 10 into 4 with cost |1−2|+|10−4|=1+6=7 and the resulting lengths [2,4,4] are almost good for t=3 .
In the second example, the sticks lengths are already almost good for t=2 , so we don't have to do anything.
题解 :
将 a 变成 b 后 需要花费 | a-b | , 并且满足一个 t ,使 | b-t| <=1 , 求 出这个 t 和 最小花费 min (
);
a ( 1<= a <= 100) , 枚举 t (1 <= t <= 101), 用 t 来表示 b , -1<= b-t <=1 , 所以 , -1+t <= b <= 1+t ;
| a - b | = min ( | a - (-1+t ) | , | a- (1+t) | ) ;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#define Swap(a,b) a ^= b ^= a ^= b
using namespace std ;
const int MAX = 1005;
const int inf = 0xffffff;
typedef long long LL;
LL gcd(LL a , LL b)
{
return b== 0 ? a : gcd(b,a%b) ;
}
int a[MAX ];
int minn = 0xfffff;
int main()
{
int n ;
int r ;
cin >>n ;
int res ;
int ans = 0 ;
for(int i =1 ;i<=n ; i++)
{
cin >>a[i] ;
}
for(int t = 1 ; t <=101; t++ )
{
ans = 0 ;
for(int i = 1 ;i <=n ; i++ )
{
if(a[i]<t-1)
{
ans += t - 1 -a[i] ;
}
else if ( a[i] >t +1)
{
ans+= a[i] - (t+1) ;
}
}
if(ans < minn )
{
minn = ans ;
res = t ;
}
}
cout<<res <<" " <<minn<<endl ;
return 0 ;
}