Codeforces Round #533 (Div. 2)A. Salem and Sticks[思维,数学,水题]

本文探讨了一种算法,旨在解决将一组棍子的长度调整到特定整数值附近的问题,以最小化调整成本。通过遍历所有可能的目标长度,算法计算了每根棍子调整到目标长度或其相邻长度的成本,并确定了最低总成本及其对应的目标长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Salem and Sticks

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值