A. Salem and Sticks
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题意:给你一个含有N个数的数组,让你找一个数x,使x的数组求值消耗的能量尽量小。

数组对于x的求值是把数组的每一个数削成| x-a[i] | <=1 ,并且a[i]数值变化1就要消耗1能量。

思路:

数据范围很小,直接暴力从1到max ( a[i] ) +2 试一试,找出那个最小的就行了,

细节看代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== "<<x<<" =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int ans=inf;
int cnt=inf;
int tt;
int tcnt;
int main()
{
gbtb;
cin>>n;
repd(i,,n)
{
cin>>a[i];
}
repd(i,,)
{
tt=i;
tcnt=;
repd(j,,n)
{
if(abs(tt-a[j])<=)
{ }else
{
tcnt+=abs(tt-a[j])-;
}
}
if(tcnt<cnt)
{
cnt=tcnt;
ans=tt;
}
}
cout<<ans<<" "<<cnt;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Codeforces Round #533 (Div. 2) A. Salem and Sticks(暴力)的更多相关文章

  1. Codeforces Round #533(Div. 2) A.Salem and Sticks

    链接:https://codeforces.com/contest/1105/problem/A 题意: 给n个数,找到一个数t使i(1-n)∑|ai-t| 最小. ai-t 差距1 以内都满足 思路 ...

  2. Codeforces Round #533 (Div. 2) A. Salem and Sticks(枚举)

    #include <bits/stdc++.h> using namespace std; int main() { int n;cin>>n; int a[n];for(in ...

  3. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  4. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  5. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  6. Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索

    Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx ...

  7. Codeforces Round #533 (Div. 2) Solution

    A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...

  8. Codeforces Round #533 (Div. 2) 部分题解A~D

    A. Salem and Sticks 题目描述 Salem gave you n n n sticks with integer positive lengths a1,a2,…,an a_1, a ...

  9. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

随机推荐

  1. 查看iPhone电池寿命

    iBackupBot 软件:iBackupBot for iTunes (收费软件) 官网:http://www.icopybot.com/download.htm iBackupBot for iT ...

  2. fedora 29 桌面版 设置 cockpit 自动开机启动

    systemctl enable cockpit 时,会出现如下错误: The unit files have no installation config (WantedBy, RequiredBy ...

  3. IE6浏览器无法打开QQ邮箱

    原因:未启用TLS1.0 解决方法: 打开IE浏览器,依次打开 [Internet]→[高级],在 设置 选项卡中,勾选[使用TLS1.0],然后点击[确定]保存修改,重启浏览器即可.

  4. 创建属于其他Session的进程

    创建其他Session(User)的进程需要拿到对应Session的Token作为CreateProcessAsUser的参数来启动进程. 修改有System权限的Token的TokenId为其他Se ...

  5. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置)

    目录 1. PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置) 1.1. 需要的软件 1.2. 启动 proxy dhcp 服务 1.3. 关键的几个配置文件 PXE 自动安装物理机 ( ...

  6. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

  7. maven中可以直接引用的java系统属性和环境变量属性

    一.查看命令: 1 mvn help :system 二.引用 在pom文件中通过 ${变量名}来引用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  8. python第七十课——python2与python3的一些区别

    1.性能:py3.x起始比py2.x效率低,但是py3.x有极大的优化空间,效率正在追赶 2.编码:py3.x原码文件默认使用utf-8编码,使得变量名更为广阔 中国='CHI' print(中国) ...

  9. flash设置里面:您的 Flash 设置会一直保留到您退出 Chrome 为止。

    疑问:flash设置里面:您的 Flash 设置会一直保留到您退出 Chrome 为止. 我记得以前的版本配置后就一直用啊,允许的就可以一直允许,现在这个sb版本退出后就恢复到默认,允许列表的网站就清 ...

  10. Python代码分行问题

    可以用“\”符号把一行过长的Python代码分解成几行,多个语句也可以写在同一行,语句之间要用“;”隔开.